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:
Escaping special characters from regular expressions

 



devkpict
New User

Mar 16, 2009, 1:42 AM

Post #1 of 7 (4590 views)
Escaping special characters from regular expressions Can't Post

I am reading /etc/fstab file,to remove some of the entries. I am applying regex like

Code
if($line =~ m/$_[0]/g) 
{
#do nothing
} else {
print FILE "$line";
}

where $_[0] contains keyword to search

there is a keyword entry /abc/pq..
when I am hoping the line having above entry will be removed, it is removing
/abc/pqrs entry also

I used quotameta but its not working


FishMonger
Veteran / Moderator

Mar 16, 2009, 5:02 AM

Post #2 of 7 (4580 views)
Re: [devkpict] Escaping special characters from regular expressions [In reply to] Can't Post

print FILE $line unless $line =~ m!$_[0]!;


devkpict
New User

Mar 16, 2009, 6:16 AM

Post #3 of 7 (4573 views)
Re: [FishMonger] Escaping special characters from regular expressions [In reply to] Can't Post

Thanks a lot,
But this doesn't seem to be working
I am still getting the same problem
when i say remove entry like ab..
it is removing abcd entry also


FishMonger
Veteran / Moderator

Mar 16, 2009, 8:11 AM

Post #4 of 7 (4570 views)
Re: [devkpict] Escaping special characters from regular expressions [In reply to] Can't Post

Please post a few of the lines and tell me which ones you want to keep and which ones you want to skip.


1arryb
User

Mar 16, 2009, 10:57 AM

Post #5 of 7 (4564 views)
Re: [devkpict] Escaping special characters from regular expressions [In reply to] Can't Post

Hi dev,

1. You might want to use an alternative regex delimiter since you are matching on strings containing the default '/', although you imply that your code compiles, so maybe that's irrelevant in this case:

Code
if ($line =~ m|$_[0]|g ) 
...


2. Of course m|/abc/pg| matches '/abc/pqrs', or anything else before or after. You'll have to make the pattern more specific to your data. Hints:

Code
# Constrain match to the end of the line. 
m|$_[0]$|
# Constrain match to the end of the word.
m|$_[0]\s+|
# Constrain match to a trailing character delimiter, like another slash.
m|$_[0]/|

etc.

Cheers,

Larry


devkpict
New User

Mar 16, 2009, 9:59 PM

Post #6 of 7 (4555 views)
Re: [1arryb] Escaping special characters from regular expressions [In reply to] Can't Post

Hi thanks a lot,

I used index instead of regex it semms to be working
matching exact sub string,do you see any flaws in
using index


1arryb
User

Mar 19, 2009, 9:45 AM

Post #7 of 7 (4168 views)
Re: [devkpict] Escaping special characters from regular expressions [In reply to] Can't Post

Hi dev,

Not at all. In fact, this is the preferred technique for parsing structured data.

Cheers,

Larry

 
 


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

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