Daniel Cosio
Daniel Cosio

Reputation: 293

finding text between words on multiple lines

In the text below Im trying to identify which family member the text belongs too. The comment will preceded the family member.. So in the sample below I should only identify the Mother.. the father does not have a comment

some stuf Father 
more stuff Mother

Comment: Deceased

After searching and trying some negative lookaround I still can't seem to get it to work.. So for example..

Father(?!Mother).*Comment:\s?(deceased|died)

This regex should NOT find a match.. But it ends up matching

Father 
more stuff Mother

Comment: deceased

If I switch Mother and Father it matches correctly.. But I need to not match in the example because the comment does not belong to the Father

Upvotes: 1

Views: 32

Answers (1)

The fourth bird
The fourth bird

Reputation: 163632

You have to extend the negative lookahead (?!.*?Mother) to match any character 0+ times or else it would assert what is directly to the right is not Mother.

In this case Mother does not follow directly after Father so the assertion succeeds and this part .*Comment will match until the last occurrence of Comment where what follows can match.

Your pattern could look like:

Father(?!.*?Mother).*Comment:\s?(deceased|died)

Regex demo

Perhaps you might make the dot star approach for the Comment no greedy as well to prevent over matching:

.*?Comment

Upvotes: 1

Related Questions