Home > Enterprise >  Given string filter out unique element from string using regex
Given string filter out unique element from string using regex

Time:05-23

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, 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

CodePudding user response:

So I updated the regex to .AA\/BHJ.S,\d \d*(\.\d )* and from my understanding it's working now.

CodePudding user response:

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.

CodePudding user response:

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

  • Related