LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 04-01-2011, 12:16 PM   #1
mrm5102
Member
 
Registered: Apr 2011
Location: Philadelphia
Posts: 158

Rep: Reputation: 3
Help building a regular expression for use in split function?


Hey all,

I am a semi-new user to linux and could use some help building a regular expression for use in a split function.

Ok, so I have a perl script that contains an array like this:

@hostNames = (ABC123R:192.168.1.1, CBA321CBP:192.168.1.2, ZYX987R:192.168.1.3, etc...)

Where for example the first element "ABC123R:192.168.1.1":
ABC123R is the hostname and 192.168.1.1 is it's IPaddress.

I am trying to write a regular expression that will split the element with a '-' wherever there is a LETTER next to a NUMBER, like so:
ABC-123-R:192.168.1.1

I tried this expression below but am struggling with using regex for slightly complicated matching criteria:

for ($x = 0; $x < scalar(@hostNames); $x++)
{
$hostNames[$x] = split /([A-Z][0-9])/, "-"
}
 
Old 04-01-2011, 05:01 PM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,971

Rep: Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849Reputation: 849
Hello mrm5102, welcome to LQ,
Code:
#!/usr/bin/perl -w

use strict;

my $zeile="ABC123R:192.168.1.1, CBA321CBP:192.168.1.2, ZYX987R:192.168.1.3";
$zeile =~ s/([A-Z])([0-9])/$1-$2/g;
$zeile =~ s/([0-9])([A-Z])/$1-$2/g;
print $zeile, "\n";
this will work for you.

Markus

Last edited by markush; 04-01-2011 at 05:02 PM. Reason: typo
 
1 members found this post helpful.
Old 04-04-2011, 09:30 AM   #3
mrm5102
Member
 
Registered: Apr 2011
Location: Philadelphia
Posts: 158

Original Poster
Rep: Reputation: 3
Awesome, that did the trick. Thanks so much!
 
  


Reply

Tags
perl, regular expressions


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expression harkonen Programming 6 07-12-2008 01:06 PM
Do we have regular expression in C++ ? indian Programming 4 03-06-2006 10:54 AM
Regular expression datbenik Programming 1 01-05-2006 02:58 PM
Anyone know regular expression? ahhua Linux - Software 1 12-04-2003 09:13 AM
regular expression gumby Programming 3 07-15-2003 01:13 PM


All times are GMT -5. The time now is 04:45 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration