How to add a user-defined string to a regular expression

Hi, I am checking a text box that should only accept float. I use this regex

^(?:[1-9]\\d*|0)?(?:[.]\\d+)?$

to check it out. It works as expected. now I want to add a user-defined string to the regular expression so that its value does not change. is there any way?

+4
source share
1 answer

, , , , , .

, alternation:

"^(?:(?:[1-9]\\d*|0)?(?:[.]\\d+)?|MY STRING)?$"
  ^^^                            ^^^^^^^^^^^^

​​- regex

:

  • ^ -
  • (?: - ,
    • (?:[1-9]\d*|0)?(?:[.]\d+)? -
    • | -
    • MY STRING - MY STRING
  • )? - (- ?)
  • $ -

MY STRING - , , , escaping regex ( Java Pattern.quote(user_str))..

+2

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


All Articles