matshako
matshako

Reputation: 3

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?

Upvotes: 0

Views: 561

Answers (2)

stema
stema

Reputation: 92986

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.

Upvotes: 1

Drew Noakes
Drew Noakes

Reputation: 310907

Use the pattern:

Til:(.*)org\.nr

Then take the second group to get the content between the parenthesis.

Upvotes: 1

Related Questions