Search code examples
regexnotepad++substringword-boundary

How to match all groups of text that contain a specific word in a specific line?


I'm trying to create a regular expression to sift through information in a text file (Notepad++), but only if the character has a "the" in the name.

My current expression: \\{[\r\n]+id:(.\*?)+name:\s+(\bthe\b)(.*?)[\r\n]\\}

{
id:     1
locID:  A
tStart: 17:10:00
tEnd:   17:35:00
name:   the man 45
text:   Lorum Ipsum
}
{
id:     2
locID:  A
tStart: 17:11:00
tEnd:   17:12:00
name:   Frank
text:   Lorum Ipsum
}
{
id:     3
locID:  A
tStart: 17:11:00
tEnd:   17:14:00
name:   Frank
text:   Lorum Ipsum
}
{
id:     4
locID:  B
tStart: 17:51:00
tEnd:   17:56:00
name:   the woman 2
text:   Lorum Ipsum
}
{
id:     5
locID:  A
tStart: 17:11:00
tEnd:   16:11:00
name:   the man with the golden gun
text:   Lorum Ipsum
}
{
id:     6
locID:  C
tStart: 17:11:00
tEnd:   17:11:00
name:   the woman with the dragon tattoo
text:   Lorum Ipsum
}
{
id:     7
locID:  A
tStart: 17:15:00
tEnd:   17:15:00
name:   Jo
text:   Lorum Ipsum
}

What I want is to only get

{
id:     1
locID:  A
tStart: 17:10:00
tEnd:   17:35:00
name:   the man 45
text:   Lorum Ipsum
}
{
id:     4
locID:  B
tStart: 17:51:00
tEnd:   17:56:00
name:   the woman 2
text:   Lorum Ipsum
}
{
id:     5
locID:  A
tStart: 17:11:00
tEnd:   16:11:00
name:   the man with the golden gun
text:   Lorum Ipsum
}
{
id:     6
locID:  C
tStart: 17:11:00
tEnd:   17:11:00
name:   the woman with the dragon tattoo
text:   Lorum Ipsum
}

Can someone tell me what I need to do to skip the rest of the line?

My current setup returns everything, and Frank id:2 Frank id:3 the woman 2 id:4.

Thanks mickmackusa.

Solution: {[^}]*name:[^}]*\bthe\b[^}]*\}


Solution

  • The negated character classes [^}] speed everything up and avoid getting snagged on newline characters. The * can be greedy because they will be halted on name:, the, and }.

    Pattern (Demo Link):

    {[^}]*name:[^}]*\bthe\b[^}]*}
    

    Edit (escaping the curly bracket version):

    \{[^}]*name:[^}]*\bthe\b[^}]*\}