Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match with an exact string using regular expression

I have small requirement.I want to search a string with exact match. Suppose i want to search for None_1, i am searching for 'None_1' using /None_1/, but it is matching even "xxxNone" but my requirement is it should match only None_[any digit]. Here is my code

/^None_+[0-9]{?}/

So it should match only None_1 , None_2

like image 655
Reddy Avatar asked Dec 03 '25 22:12

Reddy


1 Answers

You should also anchor the expression at the end of the line. But that alone will not make it work. Your expression is wrong. I think it should be:

/^None_[0-9]+$/
  • ^ matches the beginning of a line
  • [0-9]+ matches one or more digits
  • None_ matches None_
  • $ matches the end of a line

If you only want to match one digit, remove the +.


Your original expression /^None_+[0-9]{?}/ worked like this:

  • ^ matches the beginning of a line
  • None matches None
  • _+ matches one or more underscores
  • [0-9] matches one digit
  • {? matches an optional opening bracket {
  • } matches }
like image 87
Felix Kling Avatar answered Dec 06 '25 13:12

Felix Kling