Regex to capture - or '

I'm pretty sure either @JMichaelTX or @ccstone provided me with the regex I currently use. And it has been very powerful.
How would I alter the current regex to capture a first name that has a - in it, ie. Peter-Tosh. It currently errors.
Also a last name that has a ' ie. O'Conner. That also errors.
I appreciate any help.

\w is a word character (so basically any letter, including non-ASCII letters).

But - and ' are not letters.

You can put the \w in a character class (so [\w]) and then add other characters to the class (so [-'\w]).

So replace any of the \w entries in your regex with [-'\w].

Note that the - should be the first character in the character class because otherwise it has a special meaning (in which case you should backslash it, like [\w\-'])

1 Like

@peternlewis thank you,
Expand\n([-'\w]+)\s*,\s([-'\w]+)\s*

did I enter it wrong? the above correctly allows the - but errors on the '

Does your text actually contain ' or (i.e., "dumb" or "smart" quotes)? That would make a difference in whether your regex matches or not.

2 Likes

ah, yes, that would do it wouldn't it... ? Thank you for taking the time to reply, that fixed it up perfectly.... cheers.