Regular expression for UK postcode also matches UUID

mh377 :

I am having problems with the following UK Postcode regex

([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9][A-Za-z]?))))\s?[0-9][A-Za-z]{2})

It works for UK postcodes as intended e.g.

AB11AB

However, it also seems to match UUIDs as well e.g.

c25d4f64-2336-4a5d-b94c-14dc12xxxa58

Is there anyway to ignore UUIDs from the regular expression ?

Please find example here

https://regex101.com/r/dI6gD9/19

Webber :

You are using the correct regex, that is issued by the UK government.

Below i added examples of how to use it:

Match full string:

When matching to a full string don't use the global flag, because then it will find the occurrences within a string, rather than testing a string to fully match the regex.

So don't use the global and multi-line flags

Notice the gm part in

/your_regex/gm

Try it in this example on regex101.com, where I have already disabled the global and multi-line flag for you.

Match in log file:

For log files, add the word identifier around your regex

Notice the \b parts in

/\byour_regex\b/gm

Try it in this example which shows this behaviour in an example log file.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=154431&siteId=1