1

How can I find the space before and after hyphenated words? i.e. "This is my hyphenated-word example test"

I need the space between: "my hyphenated-word" and "hyphenated-word example"

AvnerMil
  • 41
  • 1
  • 5
  • Are you ready to sacrifice words like _Ctrl-Alt-Del_ or _politico-military_? I mean, do you consider hyphenated all words that have at least one hyphen in them, with no other conditions? – Alexander Pavlov Mar 23 '12 at 17:58
  • why - do you want to just select the hyphenated-word? if not, what language - does is support look ahead / look behind? – Aprillion Mar 23 '12 at 18:00
  • What do you want to do, and why? Why don’t you just match `(\s*)(your-word-here)(\s*)`, and then look at whether groups 1 and 3 have positive length? No lookarounds required. – tchrist Mar 23 '12 at 22:21

2 Answers2

2

You can use lookahead and lookbehind.

The space before:

\s(?=([^\s]+-[^\s]+))

The space after:

 (?<=([^\s]+-[^\s]+))\s
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • +1 But it will only work in .net, because all others do not support infinite length lookbehind. – stema Mar 23 '12 at 18:05
  • @stema You are right, .NET's is the "regexp flavor" that I've been using lately. I am not sure if Javascript or Java provide similar capabilities - I've been out of these for several years, but I guess not. – Sergey Kalinichenko Mar 23 '12 at 18:08
  • see http://www.regular-expressions.info/javascript.html if you want to know that javascript does not support lookbehind... – Aprillion Mar 23 '12 at 18:11
1

in case you just want to get the hyphenated-word itself, retrieve the whole match (e.g. capturing group 0) with this regex:

\w+(?:-\w+)+
Aprillion
  • 21,510
  • 5
  • 55
  • 89