Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Regular Expression

Posted on 2006-11-19
5
Medium Priority
?
289 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 64

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 64

Accepted Solution

by:
Fernando Soto earned 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Integration Management Part 2
Screencast - Getting to Know the Pipeline
Suggested Courses

926 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