Search code examples
c#regexstring-search

Regex for beginning of line -or- last number in sentance


new Regex(@"^[a-zA-Z]+\b +\b[a-zA-Z]?\b +\b[a-zA-Z]+$")

this matches

John Smith John B Goode

I am trying to modify this regex for the following cases:

some text before 12359 (John B? Goode) 10249?

that is sometimes the name comes after the a number at the end of the string and optionally before a final number at the end.

I have tried

new Regex(@"^|[0-9]+([a-zA-Z]+\b +\b[a-zA-Z]?\b +\b[a-zA-Z]+) *[0-9]*?$")

but that does not work because

  1. the ^|[0-9]+ only matches numbers anymore and not beginning of line
  2. the group is always an empty string that match something like sometext 12354 (the first number needs to not be at the end of a line.

Update

This is all water under the bridge because I found more names at the end of the lines of data so this will not work.

However the solution to my problem was not throwing the OR in a group.


Solution

  • You need parentheses around the alternation:

    (^|[0-9]+)
    

    Your expression is equivalent to this:

    new Regex(@"^|()")
    

    It always matches the start of the string and nothing else.