Wednesday, May 15, 2024
 Popular · Latest · Hot · Upcoming
83
rated 0 times [  90] [ 7]  / answers: 1 / hits: 76564  / 9 Years ago, mon, august 10, 2015, 12:00:00

I was looking for a regex to match words with hyphens and/or apostrophes. So far, I have:



(w+([-'])(w+)?[']?(w+))


and that works most of the time, though if there's a apostrophe and then a hyphen, like qu'est-ce, it doesn't match. I could append more optionals, though perhaps there's another more efficient way?



Some examples of what I'm trying to match: Mary's, High-school, 'tis, Chambers', Qu'est-ce.


More From » regex

 Answers
23

use this pattern



(?=S*['-])([a-zA-Z'-]+)


Demo



(?=                 # Look-Ahead
S # <not a whitespace character>
* # (zero or more)(greedy)
['-] # Character in ['-] Character Class
) # End of Look-Ahead
( # Capturing Group (1)
[a-zA-Z'-] # Character in [a-zA-Z'-] Character Class
+ # (one or more)(greedy)
) # End of Capturing Group (1)

[#65476] Friday, August 7, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
amari

Total Points: 736
Total Questions: 111
Total Answers: 90

Location: Saint Pierre and Miquelon
Member since Fri, Jan 28, 2022
2 Years ago
;