3

I have a problem with writting a reg exp for a string like this in C#

String correct = "<a>link</a>";
String wrong   = "link</a>";

I know how to select the first in a reg exp example

string regExp = "^(<a>)";

Ans i know how to select the last one

string regExp = "(</a>)$";

But how could i combine this two, to one

Bham
  • 245
  • 1
  • 2
  • 10

1 Answers1

4

Please use:

Regex regex = new Regex("<a>(.*)</a>");

string correct = "<a>link</a>";    
bool okBool = regex.IsMatch(correct); // true

string wrong = "link</a>";
bool wrongBool = regex.IsMatch(wrong); //false

Or as mentioned by Ilya Ivanov, you can use this regex:

Regex regex = new Regex("^<a>(.*)</a>$");
Community
  • 1
  • 1
Odrai
  • 2,163
  • 2
  • 31
  • 62