Reputation: 313
I'm trying to catch a string containing two or more uppercase characters. The end of the string is always a number. Sometimes there might be line breaks between the letters and the number, sometimes not. Here's what I have so far:
[A-Z-ÅÖÄ\s]+[^\d]
The engine is based on javascript.
Example:
Input:
ABC ABCDE XYZ
1
EFG XYZ ABC 2
not me
EFG ABC
3
Output: match1: ABC ABCDE XYZ match2: EFG XYZ ABC match3: EFG ABC
Upvotes: 2
Views: 218
Reputation: 424983
Try this:
^[A-Z]{2}( *[A-Z])*(?=\s\d$)
See live demo.
Or this multi-lingual version (YMMV in JavaScript):
^\p{Lu}{2}( *\p{Lu})*(?=\s\d$)
See live demo.
This handles all uppercase letters from all languages (as suggested by your attempt that includes ÅÖÄ
characters).
Neither will match "ABC fail 1"
.
Upvotes: 1
Reputation: 784998
You can use this regex in Javascript:
/^[A-Z]{2}[^]*?(?=\s*\d+$)/gm
RegEx Description:
^
- Line start[A-Z]{2}
- Match 2 uppercase English alphabets[^]*?
- Match 0 or more of any characters (including newline), lazy(?=\s*\d+$)
- Lookahead that asserts we have 0 or more space followed by a 1 or or more digits in the end.Code Demo:
var str = `ABC ABCDE XYZ
1
EFG XYZ ABC 2
not me
EFG ABC
3`;
var re = /^[A-Z]{2}[^]*?(?=\s*\d+$)/gm;
var m = str.match(re);
console.log(m);
Upvotes: 2