Regular expression to match hyphenated and / or apostrophe words

I searched for a regular expression to match hyphenated words and / or apostrophes. So far I have had:

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

and it works most of the time, although if there is an apostrophe and then a hyphen, for example, "qu'est-ce", it does not match. Could I add additional options, although perhaps there is an even more efficient way?

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

+4
source share
5 answers

use this template

 (?=\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) 
+10
source

[\w'-]+ will correspond to almost any occurrence of words with (or without) hyphens and apostrophes, but also in those cases when these characters are adjacent. (?:\w|['-]\w)+ should match cases where characters cannot be adjacent.

If you need to be sure that the word contains hyphens and / or apostrophes and that these characters are not adjacent, try \w*(?:['-](?!['-])\w*)+ . But it will also match "and - only."

+2
source

How about: \'?\w+([-']\w+)*\'?

demo

I believe these words should not match:

  • something- or -something : start or end with -
  • some--thing or some'-thing : - no character follows
  • some'' : two hyphens
0
source

The problem you are facing is that you actually have three possible sub-patterns: one or more characters, an apostrophe followed by one or more characters, and a hyphen followed by one or more characters.

This suggests that you do not want to accept words that begin or end with apostrophes or hyphens or have hyphens next to apostrophes (or vice versa).

I believe the best way to submit this to RegExp is:

/ \ B [a-d] + (?:? ['-] [AZ] +) * \ b /

which is described as:

 \b # word-break [az]+ # one or more (?: # start non-matching group ['-]? # zero or one [az]+ # one or more )* # end of non-matching group, zero or more \b # word-break 

which will match any word that begins and ends with alpha, and can contain zero or more groups of either apos or a hyphen followed by one or more alpha.

0
source

All Articles