user3904534
user3904534

Reputation: 319

How do I create a regex for only 2 capital letters?

I don't have a lot experience with regex but I've trying all sorts of combination all day

Here is what I'm searching through

Continental EcoContact5 205/55 R16 0H MO
Goodyear EFFICIENTGRIP 205/55 R16 91H
Klebér DYNAXER HP3 205/55 R16 91H

and here is my regex /([A-Z]{2})/ I only want the MO on the first line but I'm getting extra garbage with my regex

Upvotes: 1

Views: 71

Answers (1)

hwnd
hwnd

Reputation: 70750

Depending on the language, you can use word boundaries \b around your pattern. A word boundary does not consume any characters, it asserts that on one side there is a word character, and on the other side there is not.

\b([A-Z]{2})\b

Alternatively, if not supported you could sort of create your own boundaries.

(?:^| )([A-Z]{2})(?: |$)

Upvotes: 3

Related Questions