Reputation:
I have strings of the following form:
en-US //return en
en-UK //return en
en //don't return
nl-NL //return nl
nl-BE //return nl
nl //don't return
I would like to return the one's that are indicated in the code above. I tried .*\-
but this returns en-
. How do I stop returning before the slash? So only return en
? I'm testing it here.
Upvotes: 1
Views: 2877
Reputation: 163207
One option is to use a capturing group at the start of the string for the first 2 lowercase chars and then match the following dash and the 2 uppercase chars.
^([a-z]{2})-[A-Z]{2}$
If you want to capture multiple chars [a-z]
(or any character except a hypen or newline [^-\r\n]
) before the dash and then match it you could use a quantifier like +
to match 1+ times or use {2,}
to match 2 or more times.
^([a-z]{2,})-
Upvotes: 3
Reputation: 11
.+?(?=-)
as a regular expression should do what you are asking.
Where
.
matches any character
+?
matches between one and infinity times, but it does it as few times as possible, using lazy expansion
and
(?=-)
Is a positive look ahead, so it checks ahead in the string, and only matches and returns if the next character in the string is -
but the return will not include the value -
Upvotes: 1
Reputation: 15120
You could use a positive lookahead.
.*(?=-)
If you are always specifically looking for 2 lowercase alpha characters preceeding a dash, then it is probably a good idea to be a bit more targeted with your regex.
[a-z]{2}(?=-)
Upvotes: 2