Solved

Regular Expression

Posted on 2006-11-19
5
269 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

9 Experts available now in Live!

Get 1:1 Help Now