Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Help with creating a Regular Expression

Posted on 2007-08-01
8
Medium Priority
?
270 Views
Last Modified: 2013-11-07
Hey all,

Trying to repost this question worded better to get an answer closer to what I want.  I'm using Visual Studio 2005, C#, .Net Framework 2.0

Here is the text i'm parsing from (just a made up sample):  "heading 90 degrees east, crossing a junction found on the south side at 14 mm, a total distance of 27 mm to the intersection"

In that example, what I need to pull out is "east 27 mm" or "east a total distance of 27 mm".  Either one will work for what i'm doing with CAD instructions.  I do NOT want to pull out "east, crossing a junction found on the south side at 14mm, a total distance of 27 mm", that really doesn't help me much.  To do that you can just do (east).*a total distance of.* or some such which isnt what I need.

Thanks in advance!
Nathan
0
Comment
Question by:ironwill96
  • 5
  • 3
8 Comments
 
LVL 2

Author Comment

by:ironwill96
ID: 19614024
Forgot to mention, in that example the "east" and "a total distance of" can be assumed to always be fixed, they will never change so you can use them as search keys.
0
 
LVL 2

Author Comment

by:ironwill96
ID: 19614098
I seem to be getting somewhere with using something like: "\ba total distance of\b\s*\d*\s*\w*" which would output for me "a total distance of 27 mm" out of that string.  The only issue is pairing it up with the east, how can I account for all the junk text in between the two areas that i'm searching for?
0
 
LVL 2

Accepted Solution

by:
crustymonkey earned 2000 total points
ID: 19614434
What you really want to do here is use groups.  I'm a little rusty on the C#, but I think it's something to this effect:

string input = "stuff you have above";
string re = @"(east|west|north|south).*?total distance[^\d]*(\d+)\s+(\w+)";
Match m = Regex.Match(input , re);
string finalString = m.Groups[1] + " " + m.Groups[2] + " " + m.Groups[3];

And the value of finalString should be exactly what you are looking for "east 27 mm".  Note that this accounts for different directions (north, south, west).
0
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.

 
LVL 2

Author Comment

by:ironwill96
ID: 19619496
Thanks I will check this tonight when I get home and see if it works as I was wanting.
0
 
LVL 2

Author Comment

by:ironwill96
ID: 19621015
Tested the regex some and with some tweaks it seems to pull some of the right area, but then i'd need post processing like you are doing in C#..  Ideally that shouldn't happen because it is a large input file with lots of other matches so knowing when to employ "special case #1" or what not is going to be extremely difficult and another coding nightmare in and of itself.
0
 
LVL 2

Author Comment

by:ironwill96
ID: 19621980
Thanks, that seems to be the only real way to do it is with the C# outer processing.  Its too bad you can't search using a backreference by doing a regex on the contents of an overall backreference from within an expression (i.e. do what the groups do but with only regex to do it).
0
 
LVL 2

Expert Comment

by:crustymonkey
ID: 19621981
If that is the case, then yes, it would have to be changed but I wrote that based on what you said you were trying to do.  Without using groupings, there is no way you can extract "east 27 mm" with a regular expression.  I obviously don't know what the variations are in the lines you are trying to extract, but regular expressions can be very, very flexible in getting only what you want when you know how to write them.
0
 
LVL 2

Expert Comment

by:crustymonkey
ID: 19621987
You can do back references and forward references actually within regular expressions.  The only thing that makes it so it will require the group processing is the fact that that is the only way to take pieces out of a string.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses
Course of the Month12 days, 11 hours left to enroll

578 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