princess of persia
princess of persia

Reputation: 2232

Regex Match first occurrence

I have a huge text file containing various articles and I am trying to extract lines from it using regex. I know that the line below the title starts either with For/From (most of the times) I wrote the following regex but if there are articles that have paragraphs starting with From/For, it matches the title until the paragraph. How do I make the regex match the very first occurrence of For/From?

((?<=\n)[A-Z].*\n+(?=(?:(?:From)|(?:For))))

Upvotes: 1

Views: 1158

Answers (2)

protist
protist

Reputation: 1220

It seems likely that you are using the /g flag. It is possible that removing that may fix your problem, as you don't want to match all occurrences, only the first one.

Upvotes: 0

Andrew Cheong
Andrew Cheong

Reputation: 30273

Make your quantifier non-greedy.

((?<=\n)[A-Z].*?\n+(?=(?:(?:From)|(?:For))))
               ^

Upvotes: 2

Related Questions