0

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

1 Answers1

2

You should not cross matching Name:

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

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70