split on special chars (++) 
Author Message
 split on special chars (++)

Having trouble with special characters in my split pattern.  I
basically want the user to specify what substring to search for and
then I do a split on the string of that substring.  If anything is
returned, I know that substring was found.

$str = "C, C++, Perl";

$matches_found = $#sections;

This results in an error:  Nested *?+ in regexp

$str = "C, C++, Perl";

$matches_found = $#sections;

This works fine, but $substring is being passed in and therefore
doesn't contain the proper formatting (\\).

Is there a way to quote or tell split to use $substring as the search
string and not the search regular expression?

TIA.
-dwc



Fri, 13 Oct 2000 03:00:00 GMT  
 split on special chars (++)

Note the \Q

#!/usr/local/bin/perl -w
use strict;

$str = "C, C++, Perl";

$matches_found = $#sections;


Quote:

> $str = "C, C++, Perl";
> $substring = "C++";

> $matches_found = $#sections;

> This results in an error:  Nested *?+ in regexp

> Is there a way to quote or tell split to use $substring as the search
> string and not the search regular expression?

---
Brad Baxter, UGA


Fri, 13 Oct 2000 03:00:00 GMT  
 split on special chars (++)


Quote:
>Having trouble with special characters in my split pattern.


if you have an interpolation with  regular expression and you don't
want the meta-characters to be meta-characters, you can use the
\Q and \E thingys.  \Q says "characters up to \E (or the end if there
is no \E) aren't special".  

   m/\Q$substring\E/;

you can also use handy functions like quotemeta() and such.

see the Camel, the FAQs, and previous posts on this topic for more
info.

good luck :)

--

CGI Meta FAQ <URL:http://computerdog.com/CGI_MetaFAQ.html>
Comprehensive Perl Archive Network (CPAN) <URL:http://www.perl.com>
Perl Mongers <URL:http://www.pm.org>



Fri, 13 Oct 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Replacing special chars with special chars

2. about special chars when split()

3. Shell special chars in file names?

4. pattern matching with special char

5. escape special chars before loading into MySQL

6. Special chars

7. special chars

8. Text::CSV with Special Chars?

9. perl/sendmail: encoding special chars

10. is there a way to escape special chars

11. Newbie - Escaping special chars in forms

12. Dealing with the special chars in the file name

 

 
Powered by phpBB® Forum Software