Home > front end >  How to get numbers after special character using regex
How to get numbers after special character using regex

Time:05-22

import re
string = "He go $200 and umm go $136.33. His ssn number:987-645-33 and the got credit:973647 with 155 percent discount "
a = re.findall(r"(?:(?<=ssn number:)|(?<=credit:)|(?<=$))[\w\d-] ",string)
print(a)

Required solution:['$200','$136.33','987-645-33','973647']

Is there any solution to get the $ values using the regex. The $ symbol should be added in list

CodePudding user response:

Here is a version which also captures dollar amounts:

import re
string = "He go $200 and umm go $136.33. His ssn number:987-645-33 and the got credit:973647 with 155 percent discount "
a = re.findall(r"(?:(?<=ssn number:)|(?<=credit:)|\$)\d (?:[.-]\d )*", string)
print(a)  # ['$200', '$136.33', '987-645-33', '973647']
  • Related