killsteenkill
killsteenkill

Reputation: 91

In regex, how to search after the first occurrence of a string?

Given I have a string

Teacher: ID: 123 Something: Something Name: ABC Age: 40 Student1: Name: XYZ Age: 12 Student2: Name: ABC

I want to check using regex that the first Name: after Teacher corresponds to ABC

I have tried to do using lazy

/(Teacher:(.*?)Name: ABC) 

but that would return true even if

Teacher: Name: EFG Age: 40 Student1: Name: XYZ Age: 12 Student2: Name: ABC because it will then take the larger string.

EDIT: added try and made more generic

Upvotes: 0

Views: 40

Answers (1)

The fourth bird
The fourth bird

Reputation: 163207

You should not cross matching Name:

Teacher:(?:(?!\bName:).)*\bName: ABC

Regex demo

Upvotes: 2

Related Questions