Reputation: 2232
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
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
Reputation: 30273
Make your quantifier non-greedy.
((?<=\n)[A-Z].*?\n+(?=(?:(?:From)|(?:For))))
^
Upvotes: 2