1

I have this String and I want to filter the digit that came after the big number with the space, so in this case I want to filter out 2 and 0.32. I used this regex below which only filters out decimal numbers, however I want to filter both decimals and integer numbers, is there any way?

String s = "ABB123,ABPP,ADFG0/AA/BHJ.S,392483492389 2,BBBB,YUIO,BUYGH/AA/BHJ.S,3232489880 0.32"

regex = .AA/BHJ.S,\d+ (\d+.?\d+)

https://regex101.com/r/ZqHDQ8/1

2 Answers 2

1

The problem is that \d+.?\d+ matches at least two digits. \d+ matches one or more digits, then .? matches any optional char other than line break char, and then again \d+ matches (requires) at least one digit (it matches one or more).

Also, note that all literal dots must be escaped.

You can use

.AA/BHJ\.S,\d+\s+(\d+(?:\.\d+)?)

See the regex demo.

Details:

  • . - any one char
  • AA/BHJ\.S, - a AA/BHJ.S, string
  • \d+ - one or more digits
  • \s+ - one or more whitespaces
  • (\d+(?:\.\d+)?) - Group 1: one or more digits, and then an optional sequence of a dot and one or more digits.
Sign up to request clarification or add additional context in comments.

Comments

0

You could look for anything following /AA/BHJ with a reluctant quantifier, then use a capturing group to look for either digits or one or more digits followed by a decimal separator and other digits.

/AA/BHJ.*?\s+(\d+\.\d+|\d+)

Here is a link to test the regex:

https://regex101.com/r/l5nMrD/1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.