Search code examples
regex-negation

Inverse of this regex expression


I have a list:

50 - David Herd (1961-1968)
49 - Teddy Sheringham (1997-2001)
48 - George Wall (1906-1915)
47 - Stan Pearson (1935-1954)
46 - Harry Gregg (1957-1966)
45 - Paddy Crerand (1963-1971)
44 - Jaap Stam (1998-2001)
43 - Paul Ince (1989-1995)
42 - Dwight Yorke (1998-2002)

I want to select all characters EXCEPT the first and last name with the space in between in order to delete them and leave just the first name, space and last name.

So far I can select the first name, space and last name with:

([[a-zA-Z]+\s[a-zA-Z]+)

But I am unsure of how to 'invert' this expression. Any pointers would be much appreciated.


Solution

  • One option is to match the surrounded data, and capture the firstname space lastname.

    In the replacement use the capture group.

    ^.*?\b([a-zA-Z]+\s[a-zA-Z]+)\b.*
    

    Regex demo