regex expression c#

Posted on 2011-04-20
Last Modified: 2012-05-11
I have a string in c# and i need a regex solution to get all matches.
An example string is:

            <div class="maintext">

I need to have all the items which start with "$/" and end with "$" so in this example "/information_page/paragraphtitle" and "/information_page/paragraphmaintext".

The items can be inside a text (not separated by spaces) and all kind of characters (a-z,0-9,/ and _) can be inside. In between the two delimeters are no spaces allowed (not necessary in regex)

Question by:Rovi31
    LVL 11

    Expert Comment

    The pattern to match the strings as requested is:  

    Open in new window

    The code to iterate the matches would be:

    using System.Text.RegularExpressions;
    foreach (Match match in Regex.Matches(yourHtmlString, @"\$/.*\$", RegexOptions.IgnoreCase))
      string val = match.Value;
      // Do something with you matched strings.

    Open in new window

    LVL 74

    Accepted Solution


    That pattern is dangerous. If we were to modify the sample input a bit:
    <h1>$/information_page/paragraphtitle$</h1><div class="maintext">$/information_page/paragraphmaintext$</div>

    Open in new window

    That is, make it one whole line (no line breaks), then the result you would actually get would be:
    $/information_page/paragraphtitle$</h1><div class="maintext">$/information_page/paragraphmaintext

    Open in new window

    I'm sure that's probably not what you want. The safer route would be something like:

    Open in new window

    string input = @"<h1>$/information_page/paragraphtitle$</h1>
                        <div class=""maintext"">
    MatchCollection matches = Regex.Matches(input, @"\$/[^$]*\$");
    foreach (Match m in matches)

    Open in new window

    LVL 74

    Expert Comment

    by:käµfm³d 👽
    My "output" from the "bad" regex should have included a trailing "$". Copy/paste didn't show me any love on that one  : )

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now