7141

Convert PHP regex to c#

Question:

I have this regex:

<DIV(?:(?!</DIV>).)*?"(http://www\.foo\.com(?:\\.|[^"\\])*)"

And I am trying to use it using c#:

@"<DIV(?:(?!</DIV>).)*?""(http://www\.foo\.com(?:\\.|[^""\\])*)"""

But this outputs every thing from <DIV onward I want it to just show inside the ""

<strong>Like the actual regex does</strong>

Answer1:

Since you are using capture groups (groups between a pair of parentheses ()), you will have to use Groups[#] to get the different parts of the capture group. Groups[1] will have the value of the first capture group.

If you are using:

@"<DIV(?:(?!</DIV>).)*?""(http://www\.foo\.com(?:\\.|[^""\\])*)"""

You will get the ones between " by using Groups[1].Value and the whole match in Groups[0].Value.

Example:

@"a(b(cd)(ef))"

Here you have 3 capture groups because there are 3 pairs of parentheses. After a match, and if you use:

Console.WriteLine(match.Groups[0].Value); Console.WriteLine(match.Groups[1].Value); Console.WriteLine(match.Groups[2].Value); Console.WriteLine(match.Groups[3].Value);

You get:

abcdef bcdef cd ef

If that's a little confusing, maybe this breakdown can help:

a(b(cd)(ef)) 1 2 3 ^--|^--| ^---------|

The numbers and ^ indicate the beginning of the capture groups.

Answer2:

<pre class="lang-cs prettyprint-override">string str = "<DIV src=\"http://www.foo.com\"></DIV>"; Regex re = new Regex(@"<DIV(?:(?!<\/DIV>).)*?\""(http:\/\/www\.foo\.com(?:\\.|[^\""\\])*)\"""); // or Regex re = new Regex(@"<DIV(?:(?!</DIV>).)*?""(http://www\.foo\.com(?:\\.|[^""\\])*)"""); Match match = re.Match(str); Console.Write(match.Groups[1]); // Returns "http://www.foo.com"

Recommend

  • Can you alter the output of %caller{0} in logback to mimic log4j %l specifier?
  • Macro to call a function
  • Anonymous functions and Maps in Scala
  • What is the difference between Google Developers Console project and a Google **Play** Developers Co
  • Regex failing to match number and dash with letter (or space and letter)
  • Need advice in designing tables in SQL-Server
  • Can I run Robotium tests automatically in parallel on multiple emulators?
  • random selection within groups
  • iPhone: 5 seconds video capture
  • How to issue a command that produces infinite output and return immediately
  • Printing out Japanese (Chinese) characters
  • Deploying pre-encrypted configuration files to a production environment
  • Regex for incomplete lines within known start and end strings
  • Each Radiobutton for each form or 1 Form for all radiobuttons?
  • What does a hyphen at end of a term mean
  • Accessing Rows In A LINQ Result Without A Foreach Loop?
  • Use sed with regex and (
  • iText RadioGroup/RadioButtons across multiple PdfPCells
  • Python ImageIO Gif Set Delay Between Frames
  • Salesforce Different WSDL files and when to use
  • Invalid Date on validation Date of js
  • Android application: how to use the camera and grab the image bytes?
  • presentShareDialogWithParams posts to FB wall, but callback handler results say error
  • How integrated is Collada to OpenGL ES
  • Time complexity of a program which involves multiple variables
  • Checking free space on FTP server
  • FileReader+canvas image loading problem
  • Insert into database using onclick function
  • Deselecting radio buttons while keeping the View Model in synch
  • Ajax jQuery multiple calls at the same time - long wait for answer and not able to cancel
  • Read text file and split every line in MSBuild
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • Sending data from AppleScript to FileMaker records
  • Return words with double consecutive letters
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Load html files in TinyMce
  • embed rChart in Markdown