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 ?
Asked
Active
Viewed 44 times
0
-
1Can you show the code that you tried? – Naftali Apr 06 '16 at 16:19
-
1`/^Roma$/i` include the start and end identifiers and case insensitive flag – ʰᵈˑ Apr 06 '16 at 16:20
3 Answers
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 :
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/
-
@ʰᵈˑ you edited the meaning of the answer. Leave that up to the poster please. – Naftali Apr 06 '16 at 16:22
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