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?

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

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...)

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...

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.