QueryQuasar
QueryQuasar

Reputation: 171

Regex matches substring a wrong string

I'm trying to write a regex that matches capital and short letters , starts with a capital letter, optionally has an apostrophe and could accept accented characters.

[A-Z]+\'?[a-zA-Z\u00C0-\u017F]*

Example of strings that has to be matched: Jon Ross James Smith John Wayne Jay Wualà

The problem is that it matches substrings in wrong strings, as John-Wayne It only matches "John" and "Wayne" but I want the entire string not matched. What am I wrong? Thank you!

Upvotes: 0

Views: 97

Answers (1)

Jack Bashford
Jack Bashford

Reputation: 44087

Use $ and ^ to match beginning and end of string:

^[A-Z]+\'?[a-zA-Z\u00C0-\u017F]*$

This will mean it'll only match the entire string.

Upvotes: 1

Related Questions