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:
Reg ex for English (United States)

 



hydpm
User

Jun 11, 2008, 2:20 PM

Post #1 of 5 (4026 views)
Reg ex for English (United States) Can't Post

   

Hi,

$Language = English (United States) [It may be English (United Kingdom) some times]

English (United States) is present in my data input , but the below condition not working.

if ($_ =~ /$Language/ ) {

}



When i replace manually AS below it works

if($_ =~ / English \(United States \) /) {

}



I need a generalized reg ex to work for these if i can search and replace "(" and ")" as "\(" and "\)".

If there is any function to add slashes for the special characters or some mechanism like that helps me.



Can some one help me for this?

Thanks

Raj


KevinR
Veteran


Jun 11, 2008, 9:13 PM

Post #2 of 5 (4013 views)
Re: [wingsof5r] Reg ex for English (United States) [In reply to] Can't Post


Code
if ($_ =~ /\Q$Language\E/ ) { 

}


You can look up \Q and \E in the perl documentation or search for them online. Basically the \Q escapes all none word characters (but not '$' so you can use it with a scalar) and the \E tells the regexp where to stop the escaping.
-------------------------------------------------


hydpm
User

Jun 12, 2008, 12:11 PM

Post #3 of 5 (3992 views)
Re: [KevinR] Reg ex for English (United States) [In reply to] Can't Post

Thanks for the help.

But i made that work as below:

$Language =~s/[\(]/\\(/;
$Language =~s/[\)]/\\)/;

But i am working it to make it in one single serch and replace expression.

I appreciate your help on this.



Thanks,

Raj


KevinR
Veteran


Jun 12, 2008, 2:18 PM

Post #4 of 5 (3988 views)
Re: [wingsof5r] Reg ex for English (United States) [In reply to] Can't Post

Your way could be written like so:

$Language =~s/([()])/\\$1/g;

But \Q is for this exact purpose and should be used.
-------------------------------------------------


(This post was edited by KevinR on Jun 12, 2008, 2:21 PM)


hydpm
User

Jun 13, 2008, 7:12 PM

Post #5 of 5 (3917 views)
Re: [KevinR] Reg ex for English (United States) [In reply to] Can't Post

Kevin,



Thanks for the help.



Raj

 
 


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

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