The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Perl Programming
|
Simple Regular Expression Q
Discuss Simple Regular Expression Q in the Perl Programming forum on Dev Shed. Simple Regular Expression Q Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 26th, 2009, 05:06 AM
|
|
Registered User
|
|
Join Date: Oct 2009
Posts: 2
Time spent in forums: 25 m 33 sec
Reputation Power: 0
|
|
|
Simple Regular Expression Q
Hello to all community,
I am newbie to Perl & Regular Expresiions too and I was looking
for an answer to the following problem...
I am trying to read a line of the form: xxyyzzz.pdf or xx.yy.zzz.pdf etc
Originally I used $line=~ s/^.*\///; to capture the filename but
this doesn't work in the second case, when dot(s) are inside the filename.
How I can do that with a regular expression ?
many thanks,
John Ch.
|

October 26th, 2009, 05:17 AM
|
 |
kill 9, $$;
|
|
Join Date: Sep 2001
Location: Shanghai, An tSín
|
|
|
The regexp you've posted removes everything up to (and including) the first '/' character in the string. There aren't any '/' characters in the filenames you've posted.
|

October 26th, 2009, 05:26 AM
|
|
Registered User
|
|
Join Date: Oct 2009
Posts: 2
Time spent in forums: 25 m 33 sec
Reputation Power: 0
|
|
|
Regexp newbie
ok let me reformylate my question:
Assuming that I have strings of the form xx.yy.zzz.pdf
Which is the regexp to get the filenames including dots (i.e capture the "xx.yy.zzz") ?
thanks ishnid for your immediate answer!
|

October 26th, 2009, 05:37 AM
|
 |
kill 9, $$;
|
|
Join Date: Sep 2001
Location: Shanghai, An tSín
|
|
You could do this:
Code:
my ( $no_extension ) = $filename =~ /^(.+)\./;
Or if they always end in .pdf you could just strip that from the end directly.
The main problem with capturing everything up to the last dot in your string is that you can get some filenames with double file extensions, such as "foo.tar.gz". Something to keep in mind.
|

October 26th, 2009, 12:01 PM
|
 |
wizard
|
|
Join Date: Jul 2009
Location: The Great White North
|
|
Quote: | Originally Posted by jch09 ok let me reformylate my question:
Assuming that I have strings of the form xx.yy.zzz.pdf
Which is the regexp to get the filenames including dots (i.e capture the "xx.yy.zzz") ?
thanks ishnid for your immediate answer! |
It would be better to use a module for it. Both File::Basename and File::Spec could and both are standard modules; they come with Perl.
__________________
__END__
I love Perl; it's the only language where you can bless your thingy.
|

October 26th, 2009, 12:14 PM
|
 |
kill 9, $$;
|
|
Join Date: Sep 2001
Location: Shanghai, An tSín
|
|
|
Don't you need to expressly supply a suffix (i.e. '.pdf' in this case) for File::Basename?
|

October 26th, 2009, 02:13 PM
|
|
|
Quote: | Originally Posted by ishnid Don't you need to expressly supply a suffix (i.e. '.pdf' in this case) for File::Basename? |
No, you can use a regex.
Taken from the perldoc.
Code:
If @suffixes are given each element is a pattern (either a string or a qr//) matched against the end of the $filename. The matching portion is removed and becomes the $suffix.
# On Unix returns ("baz", "/foo/bar/", ".txt")
fileparse("/foo/bar/baz.txt", qr/\.[^.]*/);
|

October 26th, 2009, 02:49 PM
|
 |
wizard
|
|
Join Date: Jul 2009
Location: The Great White North
|
|
Quote: | Originally Posted by FishMonger No, you can use a regex.
Taken from the perldoc.
Code:
If @suffixes are given each element is a pattern (either a string or a qr//) matched against the end of the $filename. The matching portion is removed and becomes the $suffix.
# On Unix returns ("baz", "/foo/bar/", ".txt")
fileparse("/foo/bar/baz.txt", qr/\.[^.]*/);
|
Wouldn't that be?
perl Code:
Original
- perl Code |
|
|
|
# On Unix returns ("baz", "/foo/bar/", ".txt") fileparse ("/foo/bar/baz.txt", qr/\. [^. ]*$/ );
|

October 31st, 2009, 04:49 AM
|
|
Registered User
|
|
Join Date: Oct 2009
Posts: 4
Time spent in forums: 1 h 3 m 7 sec
Reputation Power: 0
|
|
|
How to convert string to an expression in PERL
I have a quesion for my problem with using mysql and perl programming.
- data from mysql is logic expression. After query I get it's value on string format:
my $data = "$sex < 1 && $age >= 17 && $age <= 40 && ',1,2,3,' =~m/(,)$optid(,)/";
In this expression $sex,$age,$optid is variables
- How to make Perl understanding this function?
my $sex,$age,$optid;
if ($data) {
........
........
}
as
if ($sex < 1 && $age >= 17 && $age <= 40 && ',1,2,3,' =~m/(,)$optid(,)/) {
........
........
}
help me please!
|

October 31st, 2009, 05:22 AM
|
 |
'fie' on me, allege-dly
|
|
Join Date: Mar 2003
Location: in da kitchen ...
|
|
you would need to parse that logic expression, and break on &&
Code:
(@atoms)=split(/&&/, $data);
$if_string="if (";
while (@atoms) {
$if_string.="($_) && ";
}
#remove the last two & and a space
$if_string=substr($if_string, 0, -3).") {";
print $if_string;
This what you're after, NOT TESTED BTW
__________________
--Ax
without exception, there is no rule ...
Handmade Irish Jewellery
Targeted Advertising Cookie Optout (TACO) extension for Firefox
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones
 
09 F9 11 02
9D 74 E3 5B
D8 41 56 C5
63 56 88 C0
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. -- Jamie Zawinski
Deta vil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
BIT COINS ANYONE
|

October 31st, 2009, 05:23 AM
|
 |
'fie' on me, allege-dly
|
|
Join Date: Mar 2003
Location: in da kitchen ...
|
|
something seems to have gone a tad arseways here, these two threads just collided??? 
|

October 31st, 2009, 05:54 AM
|
|
Registered User
|
|
Join Date: Oct 2009
Posts: 4
Time spent in forums: 1 h 3 m 7 sec
Reputation Power: 0
|
|
|
How to convert string to an expression in PERL
Quote: | Originally Posted by Axweildr you would need to parse that logic expression, and break on &&
Code:
(@atoms)=split(/&&/, $data);
$if_string="if (";
while (@atoms) {
$if_string.="($_) && ";
}
#remove the last two & and a space
$if_string=substr($if_string, 0, -3).") {";
print $if_string;
This what you're after, NOT TESTED BTW |
------------------------------------------------------------
Thank for your helped, but I want to make that perl understanding
my $data = "$sex < 1 && $age >= 17 && $age <= 40 && ',1,2,3,' =~m/(,)$optid(,)/";
my $sex,$age,$optid;
if ($data) {
........
........
}
Likes
if ($sex < 1 && $age >= 17 && $age <= 40 && ',1,2,3,' =~m/(,)$optid(,)/) {
........
........
}
Ex:
Code:
my $age=1;#wrong data for expression -> return fales
my $sex = 0;
my $optid = 100;#Wrong data for expression -> return fales
my $data = "$sex < 1 && $age >= 17 && $age <= 40 && ',1,2,3,' =~m/(,)$optid(,)/";
if ($data) {
print "True1";
} else {
print "False1";
}
if ($sex < 1 && $age >= 17 && $age <= 40 && ',1,2,3,' =~m/(,)$optid(,)/) {
print "True 2";
} else {
print "false 2";
}
I wanted the same value printed as
false 1
false 2
But the first expression - $data is a string and is defined so the result is "True 1"
True 1
false 2
your final result is string too, not my target
|

October 31st, 2009, 06:09 AM
|
 |
'fie' on me, allege-dly
|
|
Join Date: Mar 2003
Location: in da kitchen ...
|
|
perl Code:
Original
- perl Code |
|
|
|
#!/usr/bin/perl $data="test2"; if ($data) { } else { } $data="x"; if ($data) { } else { } $data=""; if ($data) { } else { }
The code above shows what's happening to your if statements, you need to eval your if statement in an if block as per below
perl Code:
Original
- perl Code |
|
|
|
my $data = '$sex < 1 && $age >= 17 && $age <= 40 && \',1,2,3,\' =~m/(,)$optid(,)/'; print "=====================================\n"; (@atoms)= split(/&&/, $data); $if_string="if ("; for (@atoms) { $if_string.="($_) && "; } #remove the last two & and a space $if_string= substr($if_string, 0, - 3). ") {\n"; $if_string.=" print \"True2\"; } else { print \"false 2\"; } ";
Hope that helps
--Ax
|

October 31st, 2009, 06:13 AM
|
|
|
|

November 1st, 2009, 09:10 PM
|
|
Registered User
|
|
Join Date: Oct 2009
Posts: 4
Time spent in forums: 1 h 3 m 7 sec
Reputation Power: 0
|
|
|
How to convert string to an expression in PERL
Quote: | Originally Posted by Axweildr
perl Code:
Original
- perl Code |
|
|
|
Hope that helps
--Ax |
It works - thank you very much!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|