Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Regular Expression

Posted on 2006-11-19
5
Medium Priority
?
283 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

722 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