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

Regex improvement 1

Status
Not open for further replies.

travs69

MIS
Joined
Dec 21, 2006
Messages
1,431
Location
US
Ok.. this works.

Code:
[gray]#!/usr/bin/perl[/gray]

[blue]@list[/blue] = [red]qw([/red][purple]_123_._45_.67.890 123.45.67.890 _123_._45_.___67_.___890 _123_._45_.67.890 _123_._45_._67._890___[/purple][red])[/red][red];[/red]
[olive][b]for[/b][/olive] [red]([/red][blue]@list[/blue][red])[/red] [red]{[/red]
    [olive][b]if[/b][/olive] [red]([/red][blue]$_[/blue] =~ [red]/[/red][purple]([purple][b]\d[/b][/purple]{1,3})(.[purple][b]\.[/b][/purple]|[purple][b]\.[/b][/purple]|.[purple][b]\.[/b][/purple].|[purple][b]\.[/b][/purple].)([purple][b]\d[/b][/purple]{1,3})(.[purple][b]\.[/b][/purple]|[purple][b]\.[/b][/purple]|.[purple][b]\.[/b][/purple].|[purple][b]\.[/b][/purple].)([purple][b]\d[/b][/purple]{1,3})(.[purple][b]\.[/b][/purple]|[purple][b]\.[/b][/purple]|.[purple][b]\.[/b][/purple].|[purple][b]\.[/b][/purple].)([purple][b]\d[/b][/purple]{1,3})[/purple][red]/[/red][red])[/red][red]{[/red]
        [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]1: [blue]$1[/blue] [blue]$3[/blue] [blue]$5[/blue] [blue]$7[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
    [red]}[/red]
[red]}[/red]


but I think that this part
Code:
(.\.|\.|.\..|\..)
could use some improvement (there's got to be a better way that I'm just not seeing). Also since I will never use that data is there a way to do it without capturing it into $2 $4 and $6?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[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;
 
First of all, use (?: ) for groups that you do not want to match. That would solve your primary problem.

Secondly, I believe that judicious use of the ? expression would probably serve you pretty well. And instead of '.', I would recommend that you use the \D character class to indicate "anything but a digit".

Code:
[blue]@list[/blue] = [red]qw([/red][purple][/purple]
[purple]	_123_._45_.67.890[/purple]
[purple]	123.45.67.890[/purple]
[purple]	_123_._45_.___67_.___890[/purple]
[purple]	_123_._45_.67.890[/purple]
[purple]	_123_._45_._67._890___[/purple]
[purple][/purple][red])[/red][red];[/red]

[olive][b]for[/b][/olive] [red]([/red][blue]@list[/blue][red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$_[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
    [olive][b]if[/b][/olive] [red]([/red][blue]$_[/blue] =~ [red]/[/red][purple]([purple][b]\d[/b][/purple]{1,3})(.[purple][b]\.[/b][/purple]|[purple][b]\.[/b][/purple]|.[purple][b]\.[/b][/purple].|[purple][b]\.[/b][/purple].)([purple][b]\d[/b][/purple]{1,3})(.[purple][b]\.[/b][/purple]|[purple][b]\.[/b][/purple]|.[purple][b]\.[/b][/purple].|[purple][b]\.[/b][/purple].)([purple][b]\d[/b][/purple]{1,3})(.[purple][b]\.[/b][/purple]|[purple][b]\.[/b][/purple]|.[purple][b]\.[/b][/purple].|[purple][b]\.[/b][/purple].)([purple][b]\d[/b][/purple]{1,3})[/purple][red]/[/red][red])[/red][red]{[/red]
        [black][b]print[/b][/black] [red]"[/red][purple][purple][b]\t[/b][/purple]Old: [blue]$1[/blue] [blue]$3[/blue] [blue]$5[/blue] [blue]$7[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
    [red]}[/red]
    [olive][b]if[/b][/olive] [red]([/red][red]/[/red][purple]([purple][b]\d[/b][/purple]{1,3})[purple][b]\D[/b][/purple]?[purple][b]\.[/b][/purple][purple][b]\D[/b][/purple]?([purple][b]\d[/b][/purple]{1,3})[purple][b]\D[/b][/purple]?[purple][b]\.[/b][/purple][purple][b]\D[/b][/purple]?([purple][b]\d[/b][/purple]{1,3})[purple][b]\D[/b][/purple]?[purple][b]\.[/b][/purple][purple][b]\D[/b][/purple]?([purple][b]\d[/b][/purple]{1,3})[/purple][red]/[/red][red]x[/red][red])[/red][red]{[/red]
        [black][b]print[/b][/black] [red]"[/red][purple][purple][b]\t[/b][/purple]New: [blue]$1[/blue] [blue]$2[/blue] [blue]$3[/blue] [blue]$4[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
    [red]}[/red]
[red]}[/red]

- Miller
 
Thanks Miller.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[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;
 
Code:
[blue]@list[/blue] = [red]qw([/red][purple]_123_._45_.67.890 123.45.67.890 _123_._45_.___67_.___890 _123_._45_.67.890 _123_._45_._67._890___[/purple][red])[/red][red];[/red]
[olive][b]for[/b][/olive] [red]([/red][blue]@list[/blue][red])[/red] [red]{[/red]
    [olive][b]if[/b][/olive] [red]([/red][red]s/[/red][purple][purple][b]\D[/b][/purple]+[/purple][red]/[/red][purple] [/purple][red]/[/red][red]g[/red][red])[/red][red]{[/red]
        [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$_[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
    [red]}[/red]
[red]}[/red]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Similar threads

Replies
2
Views
341
  • Locked
  • Question Question
Replies
4
Views
460
  • Locked
  • Question Question
Replies
1
Views
299
  • Locked
  • Question Question
Replies
5
Views
438

Part and Inventory Search

Sponsor

Back
Top