LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-21-2004, 06:09 PM   #1
towlie
Member
 
Registered: Apr 2004
Location: U.S.
Distribution: slackware 10.0
Posts: 110

Rep: Reputation: 15
perl matching *-length strs


I'm having problems matching a variable length
string in perl using m// and parens.

I still want perl to be greedy, I just want it
to pick up every char along the way. The code
below just gives me the last char, but I would
like to get all of them between each letter.

Also, if there are any 2 consecutive letters
without digits between them, I want the match
to give me the empty string.

Anyone know what I should change my regex to?

Thanks for your help!


Code:
#!/usr/bin/perl


# in first 2 examples would like to get the entire strings
# between each of the letters
$str= 'a1b22c333d4444e55555f';

$str =~ m/[a-f]([^a-f])*[a-f]([^a-f])*[a-f]([^a-f])*[a-f]([^a-f])*/g;

print "1:$1   2:$2   3:$3   4:$4 \n\n";
	#desired output is '1:1   2:22   3:333   4:4444 ....'




$str= 'a1b23c456d7890e12345f';

$str =~ m/[a-f]([^a-f])*[a-f]([^a-f])*[a-f]([^a-f])*[a-f]([^a-f])*/g;

print "1:$1   2:$2   3:$3   4:$4 \n\n";
		#desired output is '1:1   2:23   3:456   4:7890 ....'





# also, would like to return empty string if
# any 2 letters have no separation ('abc' in this example)
$str= 'abc456d7890e12345f';

$str =~ m/[a-f]([^a-f])*[a-f]([^a-f])*[a-f]([^a-f])*[a-f]([^a-f])*/g;

print "1:$1   2:$2   3:$3   4:$4 \n\n";
	#desired output is '1:   2:   3:456   4:7890 ....'

Here is the output I get:
Code:
1:1   2:2   3:3   4:4

1:1   2:3   3:6   4:0


1:   2:   3:6   4:0

Here is what I want to get:
Code:
1:1   2:22   3:333   4:4444

1:1   2:23   3:456   4:7890


1:   2:   3:456   4:7890
 
Old 07-21-2004, 06:43 PM   #2
Lansing_Banda
LQ Newbie
 
Registered: Jul 2003
Location: Huntsville, AL
Distribution: Mandrake 9.1
Posts: 13

Rep: Reputation: 0
If all your strings are that lenght:

Code:
m/\D(\d+)\D(\d+)\D(\d+)\D(\d+)\D(\d+)\D/g
Will fix your first two problems. As for the third, I can't think right now because I am at work, I will edit this later if I get a chance.
 
Old 07-21-2004, 08:13 PM   #3
towlie
Member
 
Registered: Apr 2004
Location: U.S.
Distribution: slackware 10.0
Posts: 110

Original Poster
Rep: Reputation: 15
figured it out

Thanks Lansing_Banda!

your code showed me what my mistake was.

I needed to put the asterik inside the parentheses

Seems obvious to me now, what was I thinking?


Here is the revised code:
Code:
#!/usr/bin/perl

# revised this code to put the asterik inside the paren

$str= 'a1b22c333d4444e55555f';
$str =~ m/[a-f]([^a-f]*)[a-f]([^a-f]*)[a-f]([^a-f]*)[a-f]([^a-f]*)/g;
print "1:$1   2:$2   3:$3   4:$4 \n\n";


$str= 'a1b23c456d7890e12345f';
$str =~ m/[a-f]([^a-f]*)[a-f]([^a-f]*)[a-f]([^a-f]*)[a-f]([^a-f]*)/g;
print "1:$1   2:$2   3:$3   4:$4 \n\n";

$str= 'abc456d7890e12345f';
$str =~ m/[a-f]([^a-f]*)[a-f]([^a-f]*)[a-f]([^a-f]*)[a-f]([^a-f]*)/g;
print "1:$1   2:$2   3:$3   4:$4 \n\n";

and the correct output:
Code:
1:1   2:22   3:333   4:4444 

1:1   2:23   3:456   4:7890 

1:   2:   3:456   4:7890
 
  


Reply


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
Perl Pattern Matching Question pete1234 Programming 2 08-27-2005 10:26 AM
perl regex matching exodist Programming 2 11-15-2004 10:50 PM
Quick perl question using length Seventh Programming 4 09-08-2004 04:29 PM
pattern matching in perl ludeKing Programming 9 04-02-2004 09:53 AM
finding the length of an array in perl acid_kewpie Programming 5 09-25-2002 10:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:59 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration