Reputation: 2243
my RegEx is not working the way i think, it should.
[^a-zA-Z](\d+-)?OSM\d*(?![a-zA-Z])
I will use this regex in a javascript, to check if a string match with it.
Should match:
12345612-OSM34
12-OSM34
OSM56
7-OSM
OSM
Should not match:
-OSM
a-OSM
rOSMann
rOSMa
asdrOSMa
rOSM89
01-OSMann
OSMond
23OSM
45OSM678
One line, represents a string in my javascript.
https://www.regex101.com/r/xQ0zG1/3
The rules for matching:
OSM
if it stands alone-
OSM
OSM
I Hope someone can help.
Upvotes: 1
Views: 105
Reputation: 6768
[^a-zA-Z](\d+-)?OSM\d*(?![a-zA-Z])
[^a-zA-Z]
In regex, you specify what you want, not what you don't want. This piece of code says there must be one character that isn't a letter. I believe what you wanted to say is to match the start of a line. You don't need to specify that there's no letter, you're about to specify what there will be on the line anyway. The start of a regex is represented with ^
(outside of brackets). You'll have to use the m
flag to make the regex multi-line.
(\d+-)?
means one or more digits followed by a -
character. The ?
means this whole block isn't required. If you don't want foreign digits, you might want to use [0-9]
instead, but it's not as important. This part of the code, you got right. However, if you don't need capture blocks, you could write (?:)
instead of ()
.
\d*(?![a-zA-Z])
uses lookahead, but you almost never need to do that. Again, specifying what you don't want is a bad idea because then I could write OSMé
and it would match because you didn't specify that é
is forbidden. It's much simpler to specify what is allowed. In your case since you want to match line ends. So instead, you can write \d*$
which means zero or more digits followed by the end of the line.
/^(?:\d+-)?OSM\d*$/gm
is the final result.
Upvotes: 1
Reputation: 80639
You can use the following simplified pattern using anchors:
^(?:\d+-)?OSM\d*$
The flags needed (if matching multi-line paragraph) would be: g
for global match and m
for multi-line match, so that ^
and $
match the begin/end of each line.
Changed the (\d+-)
match to (?:\d+-)
so that it doesn't group.
Upvotes: 4