Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regex ? 1

Status
Not open for further replies.

jimmyellis

Technical User
Sep 21, 2013
3
0
0
US
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
 
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;
    }
  }
}

: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
easier to understand and modify
oh the irony... What does split on its own do?

Does it apply it to the $_ variable, which in this context is the line from <F> ?

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
OK, touché...[blush]
[tt]split[/tt] on its own splits [tt]$_[/tt] 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 [tt]chomp[/tt] is not necessary).

: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
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

 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top