Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 04-01-2011, 11:16 AM   #1
Registered: Apr 2011
Location: Philadelphia
Posts: 165

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:, CBA321CBP:, ZYX987R:, etc...)

Where for example the first element "ABC123R:":
ABC123R is the hostname and 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:

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, 04:01 PM   #2
Senior Member
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

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

use strict;

my $zeile="ABC123R:, CBA321CBP:, ZYX987R:";
$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.


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

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


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 12:06 PM
Do we have regular expression in C++ ? indian Programming 4 03-06-2006 09:54 AM
Regular expression datbenik Programming 1 01-05-2006 01:58 PM
Anyone know regular expression? ahhua Linux - Software 1 12-04-2003 08:13 AM
regular expression gumby Programming 3 07-15-2003 12:13 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:55 AM.

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