Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expressions to string in c#

Tags:

string

c#

regex

I have to use regular expressions in a c# application and I try to use it as a string to use regex.match method. My regular expression is like id="(tt[0-9]+)\|imdb and I convert it to string like "id\"(tt[0-9]+)\\|imdb" but it doesn't work. Do you have any solutions or applications that convert regexes to strings??

like image 734
Emre Canbazoğlu Avatar asked Dec 01 '25 20:12

Emre Canbazoğlu


2 Answers

You can drop the escaping and use a @ sign.

@"id=""(tt[0-9]+)\|imdb"

Note: you have to double the doublequotes

like image 54
Variant Avatar answered Dec 04 '25 09:12

Variant


Not sure what you are trying to accomplish, so I am going down a different path then the other posts.

The Regex.Match method should take the String you are trying to match against, it sounds like you are attempting to go backwards from a constructed RegEx to a String. If that is the case simply call ToString() on the RegEx to get the passed in expression.

If you are simply trying to get the expression as a String then the pattern var below would so that making use of the @ as mentioned in the other posts. Either way though, the RegEx itself should contain the pattern you want to match against.

  string text = "91919182389348487";
  string pattern = @"id=""(tt[0-9]+)\|imdb";

  Regex r = new Regex(pat, RegexOptions.IgnoreCase);
  Match m = r.Match(text);
  ...
like image 32
Aaron McIver Avatar answered Dec 04 '25 11:12

Aaron McIver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!