×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

regex ?

regex ?

(OP)
Please excuse my limited grasp of Perl and regular expressions.

I'm trying to match lines such as the following:

4 4 4 6 1/2 4 3/4 10 9 1/4 12 20 1/4

The line ends with a carriage return. The sequence is the same, with a number followed either by a fraction or another number followed by a fraction. So above, the individual sections would be:
4 4
4 6 1/2
4 3/4
10 9 1/4
12 20 1/4

The match is fine until I get to the last sequence where I only get back the 12 and 20. The 1/4 is not matched. The code is the same throughout and there are spaces accounted for between each of the sequences.The final sequence space is followed by a carriage return (this is a WORD file).

Here's my primitive code for each sequence:

(\d{0,2})\s(\d\/\d|\d{0,2} \d\/\d|\d{0,2})\s

How do I match the last sequence?
Thanks in advance

RE: regex ?

IMHO regexes are too often used to cook almost everything, where a traditional approach would be more robust and easier to understand and modify.
I would do it this way (untested)

CODE -->

while(<F>){
  my $ini=0;
  my($first,$second);
  for(split){
    if($ini==1){
      if(/\//){
        print "$first $_\n";
        $ini=0;
      }else{
        $second=$_;
        $ini=2;
      }
    }elsif($ini==2){
      if(/\//){
        print "$first $second $_\n";
        $ini=0;
      }else{
        print "$first $second\n";
        $first=$_;
        $ini=1;
      }
    }else{
      $first=$_;
      $ini=1;
    }
  }
} 

http://www.xcalcs.com : Online engineering calculations
http://www.megamag.it : Magnetic brakes for fun rides
http://www.levitans.com : Air bearing pads

RE: regex ?

OK, touché...blush
split on its own splits $_ on multiple space chars (including tabs and newlines), without including in the output null fields for multiple spaces or any starting or ending spaces (it strips off the ending newline, so chomp is not necessary).

http://www.xcalcs.com : Online engineering calculations
http://www.megamag.it : Magnetic brakes for fun rides
http://www.levitans.com : Air bearing pads

RE: regex ?

(OP)
Thanks for the responses, guys.

given the following sequence, with spaces between the digits and a newline at the end
3 2 1/2 3 2 1 1/2 1 5 1/4

why for

if ($restrace =~/^(\d{0,2}\s\w{0,4}|\d\/\d|\d{0,2}\s\d\/\d|\d{0,2})\s(.*)/){

does $1 contain 3 2 instead of 3 2 1/2 why does it not match the 2nd of the OR conditions?

(and $2 1/2 3 2 1 1/2 1 5 1/4)

I know the re works as it have proven so when I write it for the much longer actual string. The problem is that there's one case that I can't capture with the entire string so I need to break this thing up.

Thanks in advance

RE: regex ?

(OP)
Guys

Thanks for the help. I solved it; and also solved that issues that forced me to do it this way. It's not eloquent but it works.

Appreciate the responses.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close