0

My anchor tag looks like this:-

    <a href="/as" title="asd" page="as" name="asd" reference="Yes" type="relativepath">as
</a>

I tried in this way:-

 <a [^>]*?>(?<text>.*?)</a>

It is working fine when the ending anchor tag </a> supposed to be in the same line.
But in my case the ending anchor tag should come in next line.

I need a regular expression that it should supports, if the ending anchor tag is in the next line.

Suggestions welcome.

Aishu
  • 1,111
  • 4
  • 20
  • 42

2 Answers2

2

You should use the (?s) inline option:

(?s)<a [^>]*?>(?<text>.*?)</a>

See demo.

In C#, you can also use RegexOptions.Singleline option the following way:

var input = "<a href=\"/as\" title=\"asd\" page=\"as\" name=\"asd\" reference=\"Yes\" type=\"relativepath\">as\r\n</a>";
var regex = new Regex(@"<a [^>]*?>(?<text>.*?)</a>", RegexOptions.Singleline);
var result2 = regex.Match(input).Value;

Output:

enter image description here

EDIT:

This is an updated version of the regex that takes into account <a> tags that do not have attributes (which is next to impossible, but let's imagine :)), and also make it case-insensitive (who knows, maybe <A HREF="SOMETHING_HERE"> can also occur):

var regex = new Regex(@"(?i)<a\b[^>]*?>(?<text>.*?)</a>", RegexOptions.Singleline);
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
0

Just use DOTALL modifier which makes the DOT present in your regex to match even line breaks.

@"(?s)<a [^>]*?>(?<text>.*?)</a>"

OR

You could use negated character class.

@"<a [^>]*?>(?<text>[^<>]*)</a>"
Avinash Raj
  • 160,498
  • 22
  • 182
  • 229