1

Looking to match WS-810-REFERENCE-1 where the string must have -'s within it

And can't think of something to work perfectly [a-zA-Z0-9\-]+

That will match but will also match words that do not have the - character

Thought of maybe this ([a-zA-Z0-9\-]+\-)+ But that will match WS-810-REFERENCE- missing the final segment.

Thoughts?

NMGod
  • 1,937
  • 2
  • 12
  • 11

2 Answers2

0

I believe you're looking for lookahead to make sure hyphen is present in the string. You can use:

\b(?=\w*?-)[a-zA-Z0-9-]+(?= |$)

Online Demo: http://regex101.com/r/pZ6hV6

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Matching only words not entire lines, so I tried (?=.*?-)[a-zA-Z0-9-]+ but that also matches strings without hyphens – NMGod Mar 12 '14 at 05:11
  • pardon...do you know the [answer](http://stackoverflow.com/questions/22343637/normalize-paths-with-parent-directories-in-the-middle-in-bash-script) – MLSC Mar 12 '14 at 06:59
  • 1
    @MortezaLSC: sorry I don't know python. – anubhava Mar 12 '14 at 11:43
0

Used a modified version of the second attempt just to grab that extra missing section ((?:[a-zA-Z0-9]+\-)+[a-zA-Z0-9]+)

NMGod
  • 1,937
  • 2
  • 12
  • 11