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: Beginner:
A Problem Using Regular Expression String Search

 



Hash
New User

Aug 11, 2009, 4:40 AM

Post #1 of 4 (576 views)
A Problem Using Regular Expression String Search Can't Post

Hi,

I have a file (say A) containing some words on each line. I need to write a script such that the script opens file A, then reads one line (one word) at a time from file A and searches for the word in another file B. If it finds the word picked up from file A in file B, then the script starts copying an entire line starting from the match position and prints it out.

The problem is, the words in file A have characters like "|", "/", "\", "." etc. Example: "CAT|56M.67_8A".

When I put that kind of a word into a variable ($A) and then substitute the variable in a reg. exp. (Example: $B =~ /$A/), the special characters in the word ("|", ".", and "_") are INTERPRETED. But, I want to match the special characters AS THEY ARE, without interpreting them. How could I do that?

For example:

=====================================
<File B>

CAT|56M.67_8A - Description: ABCDEFGHI.

=====================================

If I search "CAT|56M.67_8A" in the file B (above) containing an additional description attached to the match text, the perl script should pick up the entire line, including the description. But, the script currently interprets "|" to mean "OR" and therefore does not find a match even when the term is present right there in file B. I don't want perl to interpret the special meaning of "|", but rather match it as-it-is.

Could anyone please help?

Thanks.


KevinR
Veteran


Aug 11, 2009, 10:22 AM

Post #2 of 4 (573 views)
Re: [Hash] A Problem Using Regular Expression String Search [In reply to] Can't Post

use \Q to escape the special characters


Code
/\Q$A/


you can end the \Q with \E if there is more stuff to match in a regexp you don't want to escape:


Code
/\Q$A\E\d+\.\d+/


You can read the perl regexp tutorials although its not easy to digest if you're a real perl beginner. You can find them here:

http://perldoc.perl.org/index-tutorials.html

or google for "perl regular expression tutorial" and you will find tons of stuff on the internet, some easier to read than others.
-------------------------------------------------


ichi
User

Aug 11, 2009, 7:23 PM

Post #3 of 4 (566 views)
Re: [Hash] A Problem Using Regular Expression String Search [In reply to] Can't Post

@OP, you don't have to use regex. if searching for strings of file A is file B is what you want, you can use the index() function. type perldoc -f index() for more info.


Hash
New User

Aug 12, 2009, 2:08 AM

Post #4 of 4 (561 views)
Re: [KevinR] A Problem Using Regular Expression String Search [In reply to] Can't Post

Hi KevinR and ichi,

Thank you so much.

I didn't knew anything about \Q & \E. This is exactly what I was looking for. I really appreciate your help KevinR.

ichi, thanks for your suggestion. I'll check out the index function too.

Regards,
Hash


(This post was edited by Hash on Aug 12, 2009, 2:12 AM)

 
 


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

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