I think this is a simple thing for a lot of you, but I have a very limited knowlegde of regex at the moment. I want to match everything except a double digit number in a string.
For example:
TEST 22 KLO4567
QE 45 C2C
LOP 10 G7G400
Now I found out the regex to match the double digit numbers: \\d{2}
Which matches the following:
TEST 22 KLO4567
QE 45 C2C
LOP 10 G7G400
Now it seems to me that it would be fairly easy to turn that regex around to match everything BUT "\\d{2}". I searched a lot but I can't seem to get it done. I hope someone here can help.
This only works if your regex engine supports look behinds:
^.+?(?=\d{2})|(?<=\d{2}).+$
Explanation:
The |
separates two cases where this would match:
This matches everything from the start of the string ( ^
) until \\d{2}
is encountered.
This matches the end of the string, from the place just after two digits.
If your regex engine doesn't support look behinds (JavaScript for example), I don't think it is possible using a pure regex solution.
You can match the first part:
^.+?(?=\d{2})
Then get where the match ends, add 2 to that number, and get the substring from that index.
You are right rejecting a search in regex is usually rather tricky.
In your case I think you want to have [^\\d{2}]
, however, this is tricky as your other strings also contain two digits so your regex using it won't select them.
I would go with this regex (using PCRE 8.36
but should work also in others):
\\*{2}\\w*\\*{2}
Explanation:
\\*{2}
.... matches "*" literally exactly two times
\\w*
.... matches "word character" zero or unlimited times
Found one regex pretty straightforward :
^(.*?[^\d])\d{2}([^\d].*?)$
Explanations :
To test this reges you can use this link
It will match the first occurence of double digit, but because OP said there was only one it does the job correctly. I expect it to work with every regex engine as nothing too complex is used.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.