CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
how come this doesnt work??

 



hanja
Deleted

Mar 27, 2001, 11:39 AM

Post #1 of 6 (3568 views)
how come this doesnt work?? Can't Post

Hi, and thanks for helping. I have a text file such as this one and I want to match certain parts of it and return the value.

--

Author (core id) : Tom
Phone number : 555-5084

--

I am trying to match the string "Author (id number)" and return its value which in this case is "Tom". This is what I wrote but it doesn't work. What am I doing wrong? Am I? Is there a good regular expression tutorial that explains how I can search for a match a specific string and returns its value?



open (theFILE, "<C:/1.txt") or die ("Can't open the file you requested");
while ($line = <theFILE>)
{
if ($line =~ /(Author (core id) :)/
{
$author = $1;
}
}




Thank you.
hanja



freddo
User

Mar 27, 2001, 12:38 PM

Post #2 of 6 (3565 views)
Re: how come this doesnt work?? [In reply to] Can't Post

Hello Hanja,

here's my little try:
$_ = "Author (core id) : Tom";
print $1 if /Author \(core id\) : (.*)/; # prints Tom

Japhy made a tool to explain regular expressions, OGRE,
it is located at http://www.crusoe.net/~jeffp/

the regex i made, /Author \(core id\) : (.*)/, works like this:
1) it looks for the string "Author (core id) : " notice i put a backslash to remove the function of the parenthesis.
2) then it takes any character except newline (the dot ".") repeated 0 or more time (the asterisk "*") the parenthesis around "(.*)" put the content in the register $1

these were my 2 cents, i hope it helps
see you




freddo
User

Mar 27, 2001, 12:41 PM

Post #3 of 6 (3565 views)
Re: how come this doesnt work?? [In reply to] Can't Post

btw, there is a tutorial right on this site:
http://tlc.perlarchive.com/articles/perl/pm0001_perlretut.shtml



Jean
User


Mar 28, 2001, 9:47 AM

Post #4 of 6 (3554 views)
Re: how come this doesnt work?? [In reply to] Can't Post

It's almost correct, except that the parentheses should be used in exactly opposite way to what you've done. Remove the parentheses from around the constant text and put them around the rest of the string.
Another thing (here they are again) is if you expect to find the parentheses in the text, precede each one with backslash '\', like \( and \)


Solution: in your code replace
if ($line =~ /(Author (core id) :)/
with
if ($line =~ /Author \(core id\) :(.*)/
or, even nicer, with
if ($line =~ /Author\s*\(core id\)\s*:(.*)/
(\s* says any number of whitespace and will match
even if there are no space characters at all - more flexible...)

That will do it.

Good luck,

Jean Spector
QA Engineer @ Extent Technologies, Ltd.
mage@lycosmail.com


hanja
Deleted

Mar 28, 2001, 11:35 AM

Post #5 of 6 (3551 views)
Re: how come this doesnt work?? [In reply to] Can't Post

Thanks for all your help. I have been reading two Perl books for the last couple of months and I still don't quite understand the concept of regular expressions. Actually I do. The only thing that converses me and give me the toughest time is the syntax of them. I only wish in PERL6, they will make this part of the language much easier to use. I'm crossing my fingers...



Jean
User


Mar 29, 2001, 6:16 AM

Post #6 of 6 (3546 views)
Re: how come this doesnt work?? [In reply to] Can't Post

Actually I'm not sure regex can become much simpler because of their power and flexibility.
Some simple actions may be performed through Perl string functions, like split, but more powerful things will require some complex system in order to allow advanced definitions, and that's what regex is.

You can try "Mastering Regular Expressions" book from O'Reily, though most things I need I've learned from regular, i.e. non-regex specific, books. My favorite is "Perl Core Language - Little Black Book" by Steven Holzner, which covers most issues covered in other books.

And I'll try to give you the hint on how I read the regex - hopefully that'll help you at least a little:
Read the regex from the beginning, character after character with quantifiers (*+?) and line begin(^)-end($) characters attached to the last character or group of characters.
For example:
regex /a\s*bc?.+d$/
I'd read that as following:
/a/ = 'a' character,
/\s*/ = any number and combination of space and tab characters - possibly none,
/b/ = 'b' character,
/c?/ = 'c' character - optional, i.e. may not exist and regex will still match,
/.+/ = any number of any characters - at least one character is required
d$ = 'd' character appears as the last character of the string.

Jean Spector
QA Engineer @ Extent Technologies, Ltd.
mage@lycosmail.com

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives