2

Here is an example of the string that can be dynamically build.

{Static String} <a href="{Dynamic Value}"><b>{Dynamic Value 2}</b></a>

example of static text <a href="http://www.exampleurl.com">example value</a>

How with in C# using Regex to find {Dynamic Value 2} or example value?

German
  • 740
  • 2
  • 10
  • 24

4 Answers4

0

You would use something like this:

using System.Text.RegularExpressions;

private string ExtractString(string sourceString)
{
    // (?<=string) is positive look-behind where you search for string before the match.
    // .* is all characters in between.
    // (?=string) is positive look-ahead where you search for string after the match.
    string pattern = "(?<=<a.*?>).*(?=</a)";
    Match match = Regex.Match(sourceString, pattern);
    return match.Value;
}

Of course, you should be implementing some kind of exception handling mechanism.

Note that this will return

<b>{Dynamic Value 2}</b>

if parsing

<a href="{Dynamic Value}"><b>{Dynamic Value 2}</b></a>

You can process the string further with other regex patterns if needed.

Nikola Malešević
  • 1,738
  • 6
  • 21
  • 44
0

Try this, you will get your desired result.

string Actualstring = "{static string}<a href='{Dynamic Value}'><b>{Dynamic Value 2}</b></a>"  string prevSplitBy = {static string};string desiredstring="";
     string FirstSplitBy = "<b>";
                    string SecondSplitBy = "</b>";
                    Regex regexprevSplit = new Regex(prevSplitBy );Regex regexFirstSplit = new Regex(FirstSplitBy);
                    Regex regexSecondSplit = new Regex(SecondSplitBy);
                  string[] StringprevSplit = regexprevSplit.Split(Actualstring );string[] StringFirstSplit = regexFirstSplit.Split(StringprevSplit[1] );
                    string[] StringSecondSplit = regexSecondSplit.Split(StringFirstSplit[1]); if(StringSecondSplit!=null){ for(int i=0 ; i <StringSecondSplit.count-1;i++)desiredstring=desiredstring+StringSecondSplit[i] }

desiredstring will have your desired string.

  • In my example you can see static and dynamic content. So I cannot split by "" because it can be multiple occurrences. I need to include static value too. – German Sep 03 '12 at 12:02
0

Option 1: Raw parsing. Not recommended.

{Static String} <a href="{Dynamic Value}"><b>{Dynamic Value 2}</b></a>

is well parsed with something like

Regex parser = new Regex(
 @"*?\<a href\=\""(?<value1>[^\""]*)\""\>\<b\>(?<value2>[^\<]*)\<\/b\>\<\/a\>");

Option 2: XML parsing. Recommended.

XElement el = XElement.Parse("<a>your long html string to parse</a>").Element("a");
string v1 = el.Attribute("href").Value;
string v2 = el.Element("b").Value;
AgentFire
  • 8,944
  • 8
  • 43
  • 90
0

People on stackoverflow seem to be suggesting http://htmlagilitypack.codeplex.com/ for parsing html and extracting values from it. It is more fault tolerant than using a regex. If using a regex you will have to change the regex if anything is changed in the strings you search.

mortb
  • 9,361
  • 3
  • 26
  • 44