Home > database >  Regular expression for words thats ignore words thats start with - or ' 1 or more
Regular expression for words thats ignore words thats start with - or ' 1 or more

Time:11-02

i try to capture words, It needs to handle words with apostrophes and dashes in them, but no word starts with these.

i tried this

(\w(?!^['-] \w)([a-zA-Z'-] ))

for example: i have the string "Hello Bob It's Mary, your mother-in-law today- but-here' 'hellp, the mistake is your parents' --Mom"

i need it to ignore the whole word (in bold), cuz it start with apostrophes and dashes but it ignore only the apostrophes and dashes. and mark all the other word, with or without apostrophes and dashes (THATS DOSENT START WITH IT) thx

CodePudding user response:

Judging by the conversation with you, the pattern that does the job is

\b(?<!['-])\w[\w'-]*

See the regex demo. Details:

  • \b - a word boundary
  • (?<!['-]) - a negative lookbehind that fails the match if there is 'or-` immediately to the left of the current location
  • \w - a word char (use [^\W\d_] if you only want to match any Unicode letter)
  • [\w'-]* - zero or more word, ' or - chars (to match letters, ' or - use (?:[^\W\d_]|['-])* instead).

CodePudding user response:

If you want to match words starting with -:

/^\-[a-zA-Z]/
  • Related