-3

Im using a regex expression on a string and i have the issue that there is nothing to the left of the string which doesnt reaccur in the string multiple times.

">something">tofind</a>

this part:

</a>

is unique in the string but the

">

part to the left is NOT

how can i make an expression take the first "> to the left of the (to be matched) value and not the first one from the start of the string

\">(.*)</a>

doesnt work properly due to that and gives me ">tofind and rightfully so.

any solution? i would like the solution to my problem be inside the expression and not additional code. due to my inabillty to hardcode a fix for every special problem i might have with strings.

thanks alot!

code processing the string

                    var regex = new Regex(regexstring);

                    var matches = regex.Matches(line);

                    foreach (var singleuser in matches.Cast<Match>().ToList())
                    {
                        allusernames.Add(singleuser.Groups[1].Value);
                    }
tim_po
  • 105
  • 10
  • As a side note, [CsQuery](https://github.com/jamietre/CsQuery) is an awesome way to process HTML in C#! – Roman Starkov Apr 30 '15 at 15:48
  • 1
    possible duplicate of [RegEx match open tags except XHTML self-contained tags](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – AeroX Apr 30 '15 at 15:51
  • Is `">something">tofind` the literal thing to process and you are looking for the text `something` within that? – ΩmegaMan Apr 30 '15 at 16:00

1 Answers1

1

You can use the following..

\">([^>]*)</a>

Explanation:

  • \"> match literal \">
  • ([^>]*) match all characters other than > ([^>] being negated set)
  • </a> match literal </a>

See DEMO

karthik manchala
  • 13,492
  • 1
  • 31
  • 55