regex - Regular expression java to extract the balance from a string -
i have string contains " dear user bal= 1,234/ ".
want extract 1,234 string using regular expression. can 1,23, 1,2345, 5,213 or 500
final pattern p=pattern.compile("((bal)=*(\\s{1}\\w+))"); final matcherm m = p.matcher(text); if(m.find()) return m.group(3); else return ""; this returns 3.
regular expression should make? new regular expressions.
you search in regex word characters \w+ should search digits \d+.
additionally there comma, need match well.
i'd use
/.bal=\s([\d,]+(?=/)./
as pattern , number in resulting group.
explanation: .* match before bal= match string "bal=" \s match whitespace ( start matching group [\d,]+ matches every digit or comma 1 ore more times (?=/) match former if followed slash ) end matching group .* matches thereaft
this untestet, should work this:
final pattern p=pattern.compile(".*bal=\\s([\\d,]+(?=/)).*"); final matcherm m = p.matcher(text); if(m.find()) return m.group(1); else return ""; according online tester, pattern above matches text:
bal= 1,234/
Comments
Post a Comment