-2

In c#, I'm struggling to create a regex that will pick up each instance of a start -> end character. start char = # end char = ##

text to evaluate: blah blah blah blah #instance1 regex needs to pick this up## blahblah #instance2 regex needs to # ignore hashes# within until it gets to the end##

So i want it to find every instance that starts with "#" and ends with "##" ignoring other hashes in between.

This needs to work across paragraphs of text and to ignore newlines, carriage returns etc. I just want everything between the start # and the end ## for each instance of that pattern.

daFunk
  • 25
  • 1
  • 7

1 Answers1

-1

try the regex below:

/#.*?##/g

Note That: ? quantifier makes the preceding quantifier lazy, causing it to match as few characters as possible.


For multiline operation

/#(.|\s)*?##/gm

For C#

Regex.Match(input, "#(.|\s)*?##", RegexOptions.Multiline)
Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72
  • Worked fine in a regex tester but not in c# :( var bodyRegex = new Regex(@"/#(.|\s)*?##/gm"); var bodyMatches = bodyRegex.Matches(newEmail.EmailBody); That returns no matches, but the newEmail.EmailBody defo has the pattern #blahblah## repeated within it. – daFunk Jan 23 '19 at 19:22
  • 1
    why do I need to delete this answer? why is it requested? – Derviş Kayımbaşıoğlu Jan 23 '19 at 19:30