Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regex: How to escape + sign?

Reply
Thread Tools

Regex: How to escape + sign?

 
 
Vito Corleone
Guest
Posts: n/a
 
      02-10-2004
Hi,

How can I escape + sign from regex? I want to change:
C++ to <b>C++</b>

Here is what I did:
#!/usr/bin/perl
$text = "C++";
$source = "Perl c++ c-- Java C-- C++";
$source =~ s#$text#<b>$text</b>#ig;
print $source . "\n";

[bk@wolverine bk]$ ./test.pl
Nested quantifiers in regex; marked by <-- HERE in m/C++ <-- HERE / at
../test.pl line 4.

How can I do that? Plese help.

--Vito
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      02-10-2004
Vito Corleone wrote:
> How can I escape + sign from regex?

------------^^^^^^

<snip>

> $source =~ s#$text#<b>$text</b>#ig;


Try:

$source =~ s#\Q$text#<b>$text</b>#ig;
-----------------^^

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
 
 
 
Vito Corleone
Guest
Posts: n/a
 
      02-10-2004
On Tue, 10 Feb 2004 11:37:44 +0100
Gunnar Hjalmarsson <(E-Mail Removed)> wrote:

> Vito Corleone wrote:
> > How can I escape + sign from regex?

> ------------^^^^^^
>
> <snip>
>
> > $source =~ s#$text#<b>$text</b>#ig;

>
> Try:
>
> $source =~ s#\Q$text#<b>$text</b>#ig;
> -----------------^^


Wow, it works! Thanks Gunnar

--Vito
 
Reply With Quote
 
J. Romano
Guest
Posts: n/a
 
      02-11-2004
Vito Corleone <(E-Mail Removed)> wrote in message news:<(E-Mail Removed) om>...

> How can I escape + sign from regex?
>
> Here is what I did:
>
> $text = "C++";
> $source = "Perl c++ c-- Java C-- C++";
> $source =~ s#$text#<b>$text</b>#ig;


Vito,

Another way to do that is to change the line:

$text = "C++";

to:

$text = quotemeta("C++");

This will escape all non-alphanumeric characters so that the '+'
signs will match only to '+' signs if placed in a regular expression.
You can even print out $text after you set it to see what it looks
like -- it might help you understand what is happening.

-- Jean-Luc
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to read strings cantaining escape character from a file and useit as escape sequences? slomo Python 5 12-02-2007 11:39 AM
vhdl textio and escape sequences Olaf Petzold VHDL 1 11-28-2005 06:22 PM
trying out escape characters Griff Perl 6 08-20-2004 08:20 PM
escape a whole regex Tassos Perl 2 12-12-2003 06:36 PM



Advertisments