2

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
  • 235
  • 1
  • 2
  • 9

1 Answers1

3

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
  • 1,915
  • 2
  • 26
  • 50