Solved

Regular Expression

Posted on 2006-11-19
5
272 Views
Last Modified: 2010-04-16
Hi,
 I'm looking for a regular expression that does the following:

input:
"adfadfadfafafasfasfafdada foo VARIABLE TEXT bar adfafdasdfaf"

desired result of regex:
"VARIABLE TEXT"

both "foo" and "bar" are constants.

here is what I currently have:
"foo(.?)bar"

unfortunately this is what it gets me:
"foo VARIABLE TEXT bar"

note the foo and bar are there and I don't want them :)
0
Comment
Question by:toby_lang
  • 2
5 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 17976675
Use the pattern "foo\s+(.*?)\s+bar".

      string input = "adfadfadfafafasfasfafdada foo VARIABLE TEXT bar adfafdasdfaf";
      string output = "";
      output = Regex.Match(input, @"foo\s+(.*?)\s+bar").Groups[1].Value;

Fernando
0
 

Author Comment

by:toby_lang
ID: 17977273
Nice work! It a bit looks like it should work for my real word example, but I can't quite get it can you tell me what I am missing?

string input = @"<a class=""bvr-title"" title=""MLT"" href=""/online/catalog/mov?Id=255948"">
adfadsfafafdafa
<a class=""bvr-title"" title=""RFSD"" href=""/online/catalog/mov?Id=255944"">
asdadfafdsfa";

Regex regex = new Regex(@"<a class=""bvr-title"" title=""\s+(.*?)\s+""");
MatchCollection matches = regex.Matches(input);
           

for (int i = 0; i < matches.Count; i++)
{
  Console.WriteLine(matches[i].Value);
}

expected output:
MLT
RFSD
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 50 total points
ID: 17979807
Hi toby_lang;

Try it this way.

      Regex regex = new Regex(@"<a\s+class=""bvr-title""\s+title=\s*""(.*?)""\s*");
      MatchCollection matches = regex.Matches(input);
           
      for (int i = 0; i < matches.Count; i++)
      {
              Console.WriteLine(matches[i].Value);
      }

The above for loop will give the following results
   
        <a class="bvr-title" title="MLT"
        <a class="bvr-title" title="RFSD"


      for (int i = 0; i < matches.Count; i++)
      {
              Console.WriteLine(matches[i].Groups[1].Value);
      }

The above for loop will give the following results

        MLT
        RFSD

Fernando
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 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