0

I need to compare the name of a city , with a list of the city by a regular expression . The problem is that if I try "Roma" , get the "Arcinazzo Romano" city (which contains ROMA ) and not the "ROMA" city. You know tell me a regular functioning ?

3 Answers3

2

You could simply check if your specific term was surrounded by white-space or was located at the beginning or ending of your string:

(^|\s)Roma($|\s)

An example of this in action might look like :

enter image description here

Or if you just explicitly wanted to check if your string was "Roma" exactly, with no other characters, just use the expression starting / ending characters :

^Roma$
Rion Williams
  • 74,820
  • 37
  • 200
  • 327
2

try using an anchor to guarantee ROMA is the beginning of the line

/^ROMA/

http://regexone.com/lesson/line_beginning_end

Naftali
  • 144,921
  • 39
  • 244
  • 303
1ak31sha
  • 501
  • 7
  • 18
0

If you want to search for the city whose name is "Roma" but not the cities whose name contains "Roma", use delimiters to check whether the specified string starts and ends with "Roma", using the ^ (start of string) delimiter and the $ (end of string) delimiter, as follows:

^Roma$
EvilTak
  • 7,091
  • 27
  • 36