Reputation: 13
I need to create a RegEx that can get me the PO BOX full information from a string, I figured that I need to get and then everything else up to the point where a full string of numbers is found (the po box number is ok, after that, only if a word is a combination of numbers and letters is ok to match, if the whole word is made out of numbers then it should stop there i.e.
From the following string:
"Po Box 321 Stn Commerce Court 123 Sample St"
I need to get everything before 123 Sample St, like this:
"Po Box 321 Stn Commerce Court "
From the following string:
"PO Box 456 Stn 1st Can Place"
I need to get everything because there is no word made out of only numbers after the PO Box number
So far I achieve to get the first case working, but I haven't yet find an expression that works for both strings,
My attempt:
Regex.Match(txtString.Text, @"PO BOX [0-9]+([\s\w][^0-9]*|\s[a-zA-Z0-9]\s)", RegexOptions.IgnoreCase)
Thank you
Upvotes: 1
Views: 144
Reputation: 107307
You can use a positive look ahead :
PO BOX [0-9]+.*(?= \d+|$)
See demo https://regex101.com/r/bK7nY2/1
Or use a negative look ahead :
PO BOX [0-9]+((?!\d+ ).)*
Demo https://regex101.com/r/bK7nY2/3
Upvotes: 1