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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex help 1

Status
Not open for further replies.

travs69

MIS
Dec 21, 2006
1,431
US
I have a regex written like this and it works fine
Code:
$data = '01:16:43.7';
($cdr{dhour}, $cdr{dmin}, $cdr{dsec}, $cdr{dfsec}) = $data =~  /(.*):(.*):(.*)(\..*)/;

print "H:$cdr{dhour}\n";
print "M:$cdr{dmin}\n";
print "S:$cdr{dsec}\n";
print "fs:$cdr{dfsec}\n";
output:
H:01
M:16
S:43
fs:.7


but I am now getting data in the format of
$data = '16:43.7';
and want
H:
M:16
S:43
fs:.7
or
$data = '43.7';
and want
H:
M:
S:43
fs:.7
I do not know how to make the regex conditional, I could always split it and hack something together but I'm always trying to learn :)

Thanks in advance.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Hi

Travis said:
I do not know how to make the regex conditional
Just use the [tt]?[/tt] quantifier meaning the previous entity repeated 0 or 1 times :
Perl:
[teal]([/teal][navy]$cdr[/navy][teal]{[/teal]dhour[teal]}[/teal][teal],[/teal] [navy]$cdr[/navy][teal]{[/teal]dmin[teal]}[/teal][teal],[/teal] [navy]$cdr[/navy][teal]{[/teal]dsec[teal]}[/teal][teal],[/teal] [navy]$cdr[/navy][teal]{[/teal]dfsec[teal]}[/teal][teal])[/teal] [teal]=[/teal] [navy]$data[/navy] [teal]=~[/teal]  [green][i]/[highlight](?:[/highlight](.*):[highlight])?[/highlight](.*):(.*)(\..*)/[/i][/green][teal];[/teal]

Feherke.
[link feherke.github.com/][/url]
 
Hi

Oops. Missed the second part.
Perl:
[teal]([/teal][navy]$cdr[/navy][teal]{[/teal]dhour[teal]}[/teal][teal],[/teal] [navy]$cdr[/navy][teal]{[/teal]dmin[teal]}[/teal][teal],[/teal] [navy]$cdr[/navy][teal]{[/teal]dsec[teal]}[/teal][teal],[/teal] [navy]$cdr[/navy][teal]{[/teal]dfsec[teal]}[/teal][teal])[/teal] [teal]=[/teal] [navy]$data[/navy] [teal]=~[/teal]  [green][i]/[highlight](?:[/highlight][highlight pink](?:[/highlight](.*):[highlight pink])?[/highlight](.*):[highlight])?[/highlight](.*)(\..*)/[/i][/green][teal];[/teal]


Feherke.
[link feherke.github.com/][/url]
 
Thank you!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top