I’m trying to find a string with three possible forms:
ab10
ab 10
ab-10
So far I have managed to create this regular expression,
/\s+[a-zA-Z]{2}[-|\s.]?\d{2,3}\s+/
that works for a text like:
start foo ab 10 end foo.
The problem appears when the searched string is at the beginning or at the end of the text like:
ab 10 end foo.
In this case there is no whitespace at the beginning. So I need to match if the string is at the beginning (or end) of the text, but I have no clue how to do it
Can someone give me a little help?
This should fix your problem:
\bmeans word boundary, and it asserts that one side (either before or after the current position) is non-word character and the other side is word character (word character as defined by\w).Note that if you have a string like
#ab10., thenab10will still be matched. But_ab10will give no match because_is a word character.In case you want to assert that the character before and after the string you want to search has to be whitespace character (as defined by
\s), OR start/end of the line. I employ the use of look-behind and look-ahead zero-width assertion.The regex above will not find any match in
#ab10.orab10.