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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

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



Mar 27, 2001, 11:39 AM

Post #1 of 6 (8428 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.


Mar 27, 2001, 12:38 PM

Post #2 of 6 (8425 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

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


Mar 27, 2001, 12:41 PM

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

btw, there is a tutorial right on this site:


Mar 28, 2001, 9:47 AM

Post #4 of 6 (8414 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) :)/
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.


Mar 28, 2001, 11:35 AM

Post #5 of 6 (8411 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...


Mar 29, 2001, 6:16 AM

Post #6 of 6 (8406 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.


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

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