0

Goal:
Using working regex no matter what how much white or any letters that should be selected.

Problem:
When I have less space between "test">" and "testtest" the the regex code works but if I have more space between "test">" and "testtest" than the regex code doesn't work.

That part of the code am I missing?

Thank you!

Regex pattern for dotnet

(?<=<p class="listing__address">)(.+?)(?=</span><span class="listing__map-link">)

Tool That am I using for regex in Internet.
http://regexstorm.net/tester

Less space:

<p class="listing__address">                                 <a onclick="LogAction('9875', 'Map')" href="https://asdf test">    testtest,     <span>5252 S&#248;reidgrend                              </span><span class="listing__map-link">lkjlkj</span></a></p>

More space:

<p class="listing__address">                                 <a onclick="LogAction('9875', 'Map')" href="https://asdf test">           
           testtest,     <span>5252 S&#248;reidgrend                              </span><span class="listing__map-link">lkjlkj</span></a></p>
HelloWorld1
  • 13,688
  • 28
  • 82
  • 145
  • 1
    `.` doesn't match `\n`. You really should use a parser: [H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – ctwheels Mar 15 '18 at 14:39
  • How do you add multiline in the regex code? – HelloWorld1 Mar 15 '18 at 14:57

1 Answers1

0

You want to tell the Regex engine to use Single Line so it ignores newlines:

var x =
    @"<p class=""listing__address"">                                 <a 
onclick=""LogAction('9875', 'Map')"" href=""https://asdf test"">           
testtest,     <span>5252 S&#248;reidgrend                              </span><span class=""listing__map-link"">lkjlkj</span></a></p>";

var regex = new Regex(
    "(?<=<p class=\"listing__address\">)(.+?)(?=</span><span class=\"listing__map-link\">)", 
RegexOptions.Singleline);

Assert.That(regex.IsMatch(x));

That solves the immediate problem, but per ctwheels' comment, parsing HTML with Regexes is a dangerous game.

Evan M
  • 2,573
  • 1
  • 31
  • 36