Nino Regular Expression - regex

I have the following text, for example:
nino&searchPhrase=jn123456&alphabetical
And I want to extract jn123456.
I've put together the following regex to extract NINOs:
(\bnino?\b.*?|Nino?\b.*?)[a-zA-Z]{2}[0-9]{6}
The problem I have is at the very end of the regex where I'm matching the last alpha character which may or may not be there.
I've tried adding the following at the end of the regex shown above without any luck:
?[a-zA-Z]{1} and
[?a-zA-Z]{1}
Could someone please look at this and let me know where I've gone wrong.
Many thanks and kind regards
Chris

You may use something like this:
^[Nn]ino&?\w*=([a-z]{2}\d{6})
which will capture "jn123456" in the first capturing group.
Demo.
If the character & can be anything else, then you may use . instead.

Related

Regex: Find value between two sections

I'm trying to solve the following problem:
I want to get the adress-data between the values: Kunde: XXXXX and Artikel:
I want to get it within the newlines for there I can use it exact like shown.
enter image description here
Can you give me a hint, how to use the right Regex?
Many thanks in advance.
(?s)(?<=Kunde:).*?(?=Artikel:)
From the image you sent this is the correct way of doing it.
(?s) may not work in your REGEX flavor. It's called "dot-all" (see how to turn on DOTALL in various languages).
I used lookbehind (?<=) and look ahead (?=) so that "Kunde:" and "Artikel:" is not included in the match.

Regex: Non fixed-width look around assertions?

My college asked my to provide him with a regex that only matches if the test-string endswith
.rar or .part1.rar or part01.rar or part001.rar (and so on).
Should match:
foo.part1.rar
xyz.part01.rar
archive.rar
part3_is_the_best.rar
Should not match:
foo.r61
bar.part03.rar
test.sfv
I immediately came up with the regex \.(part0*1\.)?rar$. But this does match for bar.part03.rar.
Next I tried to add a negative look behind assertion: .*(?<!part\d*)\.(part\0*1\.)?rar$ That didn't work either, because look around assertions need to be fixed width.
Then I tried using a regex-conditional. But that didn't work either.
So my question: Can this even be solved by using pure regex?
An answer should either contain a link to regex101.com providing a working solution, or explain why it can't work by using pure regex.
You could use lookahead to verify the one case that fails your original regex (.rar with .part part that isn't 0*1) is discredited:
^(?!.*\.part0*[^1]\.rar$).*\.(part0*1\.)?rar$
See it in action
This is an old question, but here's another approach:
(?:\.part0*1\.rar|^(?<!\.)\w+\.rar)$
The idea is to match either:
A string that ends with .part0*1.rar (ie foo.part01.rar, foo.part1.rar, bar.part001.rar), OR
A string that ends with .rar and doesn't contain any other dots (.) before that.
Works on all your test cases, plus your extra foo.part19.rar.
https://regex101.com/r/EyHhmo/2

vim regex: Match with negation in middle of pattern

I am trying to learn regular expressions in vim and have gotten stuck on the following task:
I want to make a substitute which matches all of the following lines - and similar - except for the one containing Look aXXhead. With similar I mean just anything in between the a and the head except XX.
Look ahead
Look aYhead
Look aXhead
Look aXXhead
Look aXXXhead
In case it is relevant I had my highest hopes when trying
:%s/Look a\(XX\)\#!head/*\0
:%s/Look a\(.*\&\(XX\)\#!\)head/*\0
which only matches Look ahead due to the zero width of the \(XX\)\#! I pressume.
Tries like
:%s/Look a\(XX\)\#!.*head/*\0
misses the "triple X" line.
So, how is it done?
P.S. This is my first post on stack exchange so please help and correct me in case there is better ways or places for this question.
Try with this pattern:
/\(Look aXXhead\)\#!\&Look a.*head/
It's made of tho parts:
\(Look aXXhead\)\#!
match at every position in the file BUT not at /Look aXXhead/
Look a.*head
match also /Look aXXhead/
The \& operator tells that each of these parts have to match at the same pos.
how about:
/\vLook a(XXhead)#!.*head
or without very magic:
/Look a\(XXhead\)\#!.*head

Regex to match surrounding text

I need to replace "something[anything]" with "somethingElse(anything)", the tricky part is anything could be anything, and I don't want to replace that. Is it possible to do with regex?
P.S. In real scenario I'd be using PHPStorm to look / replace it. It would help me a lot to show me what to put exactly at the find window.
Thank you!
Idea: Capture 'anything' in a capturing group in regex. Then, back reference it in the replacement.
For example: search for something[([^]]*)\] and replace with somethingElse($1).

Extract text between two given strings

Hopefully someone can help me out. Been all over google now.
I'm doing some zone-ocr of documents, and want to extract some text with regex. It is always like this:
"Til: Name Name Name org.nr 12323123".
I want to extract the name-part, it can be 1-4 names, but "Til:" and "org.nr" is always before and after.
Anyone?
If you can't use capturing groups (check your documentation) you can try this:
(?<=Til:).*?(?=org\.nr)
This solution is using look behind and lookahead assertions, but those are not supported from every regex flavour. If they are working, this regex will return only the part you want, because the parts in the assertions are not matched, it checks only if the patterns in the assertions are there.
Use the pattern:
Til:(.*)org\.nr
Then take the second group to get the content between the parenthesis.