Match word '90% 'using regex

I want the word "90%" to match my line "I have 90% of the shares in this company."

how can i write a regex for the same?

I tried something like this:

Pattern p = Pattern.compile("\\b90\\%\\b", Pattern.CASE_INSENSITIVE
    | Pattern.MULTILINE);
  Matcher m = p.matcher("I have 90% shares of this company");
  while (m.find()){
   System.out.println(m.group());
 }

but no luck.

Can anyone catch fire on this fire?

Thanks a lot Archi

+3
source share
2 answers

In the middle "% "there is no word boundary \b; why your template is not working.

Use this template instead:

"\\b90%"

see also

There are three different positions as word boundaries:

  • Before the first character in the string, if the first character is a word character.
  • , .
  • , , .

, a \b \W a \W ( ).

'%' ' ' \W, \b "% ".

+4

"" :

/^.*(90%).*$/g
+1

Source: https://habr.com/ru/post/1744325/


All Articles