Solved

Regular Expression

Posted on 2006-11-19
5
274 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 63

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 63

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…

856 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