perl regexp matching


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl regexp matching
# 1  
Old 04-14-2010
perl regexp matching

Hello,

I cannot see what's wrong in my code.
When I run code below, it just print an empty string.

PHP Code:
my $test "SWER~~ERTGSDFGTHAS_RTAWGA_DFAS.x4-234253454.in";
if (
$test = ~ m/\~{1,2}[A-Z].*[xX]4/) {
   print 
"$1\n";
}
else {
   print 
"No match...\n";

Anyone know what I'm doing wrong?

Thanks in advance!
# 2  
Old 04-14-2010
Because you ain't got a matching group () anywhere that could put something into $1.
# 3  
Old 04-14-2010
Quote:
Originally Posted by pludi
Because you ain't got a matching group () anywhere that could put something into $1.
Okay, but should it not at least run the else statment then?

I thought my regular expression would match "~~ERTGSDFGTHAS_RTAWGA_DFAS.x4" ?

Perhaps anyone can show me how I match above string?

# 4  
Old 04-14-2010
Yes, it does match. Which is why it's not moving into the else branch. But without a matching group it doesn't know what to put into $1, so it just prints an empty line.

Compare your code with this:
Code:
my $test = "SWER~~ERTGSDFGTHAS_RTAWGA_DFAS.x4-234253454.in";
if ($test = ~ m/(~{1,2}[A-Z].*[xX]4)/) {
   print "$1\n";
}
else {
   print "No match...\n";
}

# 5  
Old 04-15-2010
Quote:
Originally Posted by pludi
Yes, it does match. Which is why it's not moving into the else branch. But without a matching group it doesn't know what to put into $1, so it just prints an empty line.

Compare your code with this:
Code:
my $test = "SWER~~ERTGSDFGTHAS_RTAWGA_DFAS.x4-234253454.in";
if ($test = ~ m/(~{1,2}[A-Z].*[xX]4)/) {
   print "$1\n";
}
else {
   print "No match...\n";
}

Ah okay, now I understand Smilie

Thanks a lot.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl regexp to extract first and second column

Hi, I am trying with the below Perl one-liner using regular expression to extract the first and second column of a text file: perl -p -e "s/\s*(\w+).*/$1/" perl -p -e "s/\s*.+\s(.+)\s*/$1\n/" whereas the text file's data looks like: Error: terminated 2233 Warning: reboot 3434 Warning:... (3 Replies)
Discussion started by: royalibrahim
3 Replies

2. Shell Programming and Scripting

Perl regexp help

Hi, I have file like below: 1|1212|34353|5fdf 6575||dfgdg sfsdf |afsf||4|aasfbc|~1213~~~~~ 1|1212|34353|5fdf 6575||dfgdg sfsdf |affsf| |4|abc|~rwarw~~asa~~~123~312313 1|1212|34353|5fdf 6575||dfgdg sfsdf |afasfs||4|aasfdbc|~564564~~~~ 1|1212|34353|5fdf 6575||dfgdg sfsdf... (1 Reply)
Discussion started by: sol_nov
1 Replies

3. Shell Programming and Scripting

matching regexp

Hi, maybe it is stupid question, but is it possible to match expression like this ? : ... // ... ( there is "//" somewhere on the line and on the end of the line there ISN'T "*/" ) I've tried something like : (in SED) sed 's/\/\/' but I need "*/" not to be on the end of the line ...... (2 Replies)
Discussion started by: kolage
2 Replies

4. Shell Programming and Scripting

perl regexp: matching the parent xml tag

Hi folks. I would like to remove the full parent (outer) xml tag from a file given a matching child (inner) tag, in a bash shell. To be more specific, this is what I have so far: $ cat myFile.xml <Sometag></Sometag> <Outer> <Inner>1</Inner> </Outer> <Outer> <stuff>alot</stuff> ... (3 Replies)
Discussion started by: BatManWSL
3 Replies

5. Shell Programming and Scripting

perl regexp: no match across newlines

Hi. Here's a tricky one (at least to me): I have a file named theFile.txt (UTF-8) that contains the following: a b cWhen I execute perl -pe 's|a.*c|d|sg' theFile.txtin bash 3.2 on MAC OS X 10.6, I get no match, i.e. the result is a b cagain. Any clues why? (2 Replies)
Discussion started by: BatManWSL
2 Replies

6. Shell Programming and Scripting

perl regexp

What is the easiest way to get full address of *.jpg images from html file using perl? example: http://farm3.static.flickr.com/2397/2126443111_65a810004c.jpg (1 Reply)
Discussion started by: mirusnet
1 Replies

7. Shell Programming and Scripting

tcl: regexp matching special character

Hello Experts, Can someone help me here: I have a variable which contains a string with "". set var1 {a} set str1 {a is the element i want to match} Now "regexp $var1 $str1" does not work? ("regexp {a\} $str1" works, but var1 gets it's value automatically from another script) Is... (6 Replies)
Discussion started by: sumitgarg
6 Replies

8. Shell Programming and Scripting

perl regexp error , I cant understand what is wrong

perl regexp error , I cant understand what is wrong Hello all I have simple perl regexp that is searching for pattern in string and replace it with the same string + addition string here is what I have : my $rec = q| new Array("Attributes Management" ... (4 Replies)
Discussion started by: umen
4 Replies

9. Shell Programming and Scripting

Need Help with Perl REGEXP

I need help with a Perl regular expression. The following string blows up my program: <david(greg jim)> If I type this string, there is no problem: <david(greg_jim)> or type david(gregjim) or type <david greg jim> the CGI program does not complain. For some reason that I do not understand the... (1 Reply)
Discussion started by: mh53j_fe
1 Replies

10. UNIX for Dummies Questions & Answers

GnuWin32 sed 4.1.4 regexp matching

I am using GnuWin32 sed and am having trouble with the regexp - i.e., they don't behave the same way as in UNIX (POSIX and and all that). I have a stream of data, e.g.: 11111'222?'22'33?'333'44444'55555' I want to insert a \n after those apostrophes that are *not* preceded by a ?. ... (2 Replies)
Discussion started by: Simerian
2 Replies
Login or Register to Ask a Question