1

I have a string variable with this content:

(<b><a href="#post9736461" title="Show">Permalink</a></b>)

How can I get the 9736461 in an extra variable? The String is always the same, just the numbers changes.

EDIT:

I tried:

Tag = Regex.Replace(Tag, @"(<b><a href=\"#post");
Tag = Regex.Replace(Tag, @"" title=\"Show\">Permalink</a></b>)");
  • 3
    Use a proper library for Html parsing. See [HtmlAgilityPack](http://htmlagilitypack.codeplex.com/) for example – Steve Apr 18 '14 at 12:29
  • 1
    @Steve You don't need a HTML parsing library for this snippet. It's pure XML in syntax so could be handled with XML and XPath. – Martin Costello Apr 18 '14 at 12:30
  • If this small string is invariant except for the the digits part, I don't see any point in using a parsing library. – Robin Apr 18 '14 at 12:34
  • All you need to do is grab the digits? http://stackoverflow.com/questions/4734116/find-and-extract-numbers-from-a-string – Robin Apr 18 '14 at 12:42

3 Answers3

0
(?<=#post)(\d+)

will get you the numbers in \1

aelor
  • 10,892
  • 3
  • 32
  • 48
  • No need for backreferencing if you use a zero-width lookbehind. Also I doubt this is very clear to someone with little regex understanding. – Robin Apr 18 '14 at 12:33
0
resultString = Regex.Replace(subjectString, @"(#post\d+)""", "$1");

Match the regular expression below and capture its match into backreference number 1 «(#post\d+)»
   Match the characters “#post” literally «#post»
   Match a single digit 0..9 «\d+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “"” literally «"»
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

The following code will do:

string input = "(<b><a href=\"#post9736461\" title=\"Show\">Permalink</a></b>)";
string value = Regex.Match(input, @"(?<=#post)\d+").Value;
Ulugbek Umirov
  • 12,719
  • 3
  • 23
  • 31