What is the problem with this character Á in a regex?

Viewed 70

When I use this regex: ^(.*[ÁÉÍÓÚáéíóú].*)(.*[\w\s].*)$ to found if a string contains accented vowels I have a problem with this Á as input. It looks like Á but it doesn't.

Example:

Regex:

^(.*[ÁÉÍÓÚáéíóú].*)(.*[\w\s].*)$

Input:

BARRAGÁN

Expected: Match

Result: Match

...But with this input doesn't match:

BARRAGÁN

Expected: Match

Result: No match

If I add this Á to the regex. The input match with an A without accent mark. If I use this regex (.*[À-ÿ].*)(.*[\w\s\.].*) neither works. What could be happening with that letter?

1 Answers

The letter is a combination of the A ASCII letter and a COMBINING ACUTE ACCENT, \u0301 (from the Mark, nonspacing (\p{Mn}) category).

You may add an alternative to your current regex, that would match the allowed base letters only if they are followed with this accent mark:

^(.*(?:[ÁÉÍÓÚáéíóú]|[AEIOUaeiou]\u0301).*)(.*[\w\s].*)$
    ^^^            ^^^^^^^^^^^^^^^^^^^^
Related