Need help with a Regex substitution

P

Paul

Hi there, I am updating a Perl script and am having difficulties
understanding a string substitution in one of the subroutines (I
didn't write it - I'm just trying to update it). I'm still a novice
when it comes to Regular Expressions so I could use some help
understanding one particular regex.

Here are the 3 lines of interest:
---
1: time_line = "$1\/$2\/$3\"\t\"$4:$5 $6";
2: time_line =~ s/^0//;
3: time_line =~ s/(^\d+\/)0/\1/;
---

at line 1, the variable equals something like : "03/23/07\" \"3:05
pm"
at line 2, I expect that the first '0' is removed, so the variable now
equals : "3/23/07\" \"3:05 pm"

==> What does line 3 do??

I've tried guessing, but just can't figure it out. Something about
repeating pattern of digits.. or not .. followed by a '0'? Then..
dunno. I got nothin.

Help! Please!

TIA. Paul.
 
D

De Vliegende Hollander

The sentient life form Paul posted the following:
3: time_line =~ s/(^\d+\/)0/\1/;
--- ....

==> What does line 3 do??

(^\d+\/)

^ is start of line
\d+ is 1 or more digits
\/ is an escaped /, which is i.e. a /

what matches between the braces ( and ) gets put in $1, which is also
accessible as \1

so one or more digits from the start of the line and a / character are
matched and put in $1 (accessible via \1 too).

The 0 that follows in the regexp is outside the braces and not put in $1,
hence the \1 in the substitute part of the regexp only contains the digits
and /, without a 0 following, hence the is removed.

So it removes the 'second' zero in the string.
 
K

kens

Hi there, I am updating a Perl script and am having difficulties
understanding a string substitution in one of the subroutines (I
didn't write it - I'm just trying to update it). I'm still a novice
when it comes to Regular Expressions so I could use some help
understanding one particular regex.

Here are the 3 lines of interest:
---

I assume by time_line you mean $time_line.
1: time_line = "$1\/$2\/$3\"\t\"$4:$5 $6";
2: time_line =~ s/^0//;

Use different separator besides the slash since there is a
slash in the pattern. For eample:
$time_line =~ s {(^\d+/)0} {$1};

or combine lines 2 & 3:
$time_line =~ s {^0?(\d+/)0?} {$1};
3: time_line =~ s/(^\d+\/)0/\1/;
---

at line 1, the variable equals something like : "03/23/07\" \"3:05
pm"
at line 2, I expect that the first '0' is removed, so the variable now
equals : "3/23/07\" \"3:05 pm"

==> What does line 3 do??

I've tried guessing, but just can't figure it out. Something about
repeating pattern of digits.. or not .. followed by a '0'? Then..
dunno. I got nothin.

Help! Please!

TIA. Paul.

As Christian pointed out, line 3 removes the leading zero from the
'day' field in the date.

HTH, Ken
 
M

Mirco Wahab

Paul said:
---
1: time_line = "$1\/$2\/$3\"\t\"$4:$5 $6";
2: time_line =~ s/^0//;
3: time_line =~ s/(^\d+\/)0/\1/;

==> What does line 3 do??

time_line =~ s{(^ \d+ /) 0}{$1}x;

Aside from the correct explanations above,
a \1 on the right side of a regex is now
considered almost always wrong.

See perldoc perlre (Warning on \1 vs $1)


Regards

M.
 
D

Dr.Ruud

Mirco Wahab schreef:

($time_line = qq{$1/$2/$3"\t"$4:$5 $6}) =~
s#^0*(\d+/)0*#$1#x;

Test:

$ perl -wle'

$_ = "01 02 03 04 05 pm";

/(\S+) (\S+) (\S+) (\S+) (\S+) (\S+)/;

($time_line = qq{$1/$2/$3"\t"$4:$5 $6}) =~
s#^0*(\d+/)0*#$1#x;

print $time_line;
'
1/2/03" "04:05 pm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top