From the following string
ar-101 "ar102" ar103 "ar-104" ar-105 "ar-106"
I only want to match the words that don't start or end with quotes and have a dash between two words. So from the string I want to match ar-101
& ar-105
This is my regex so far, but it matches the words between quotes as well
\b\w [-]\w (?<!")\b
CodePudding user response:
You've got it almost alright, just put your look behind at the start of the expression.
\b(?<!")\w [-]\w \b
CodePudding user response:
The pattern \b\w [-]\w (?<!")\b
that you tried still matches the words between quotes as there is a word boundary between double quote and a word char, and a construct like \w (?<!")
is always true as you match a word character and then assert directly to the left not a "
and a word character does not match "
If you don't want quotes at the start and at the end, you can use negative lookarounds at both ends:
\b(?<!")\w -\w \b(?!")
Without using lookarounds, you can also match from and to a double quote, and then use a capture group to capture what you want to keep:
"[^"]*"|(\w -\w )