Ashton Ashton - 1 year ago 59
C# Question

Regex to match a string that contains at least 3 dashes / hyphens

I have an email body. It contains several lines of text. I need to extract the first occurrence of a string that:

  1. comes after a specific text

  2. contains at least 3 dashes

The shape of the dashed string is unknown. It may contain letters and numbers of any number, i.e.:
AA3A-123-NNN-D or 12-OOO-12455-AS

For example:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec imperdiet porta libero ac imperdiet.

Nam enim nisl: aliquam ut feugiat vitae

Specific text after which I need to search: Etiam rhoncus AAFA-12X-DDDD-12 metus risus
More text: foo

Target language is C#.

I've tried doing something like
but as you can see here I need to set the shape of the string which is not always known.

Jan Jan
Answer Source

You can use a lazy quantifier with [\s\S]:

(?:Specific\ text\ after\ which\ I\ need\ to\ search:)

The \b is a word boundary, \K deletes everything to the left from the match.
See a demo on

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download