Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Wild card in regular expression

by sathya_myl (Acolyte)
on Nov 28, 2012 at 20:59 UTC ( [id://1006098]=perlquestion: print w/replies, xml ) Need Help??

sathya_myl has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to replace a single character in a word with wild character and ended up with error

Here is my code

my $test = "Here is the there"; $test =~s/(ther*)/\<test\>$1\<\/test\>/ig; print "$test\n";

The output is "Here is <test>the</test> <test>the</test>re"

But the expected output is "Here is the <test>there</test>

My intention is this * need to represent a character after the string ther.

Replies are listed 'Best First'.
Re: Wild card in regular expression
by davido (Cardinal) on Nov 28, 2012 at 21:09 UTC

    * isn't a "wildcard" for Perl regular expressions. Your regex is saying "match and capture 'the' plus zero or more 'r' characters". Consequently, the first 'the' matches.

    Try using s/(ther.*)\b/......., for example. "Dot" (.) is the wildcard metacharacter; it matches anything except for a newline, unless you ask Perl to allow it to match the newline too.

    Update: Just in case anyone stumbles across this thread while searching for an answer:

    Reading perlrequick is a great quick-start to regular expression syntax. Follow it up with perlretut, and then perlre. Additional useful resources include perlop, perlvar, perlrecharclass, perluniprops. But perlrequick is about a half hour of reading for a 70% better understanding of regexes. Each additional document adds a few extra percentage points of knowledge to the foundation that perlrequick builds. ...and of course for the 1000% bonus, "Mastering Regular Expressions" (O'Reilly).


    Dave

Re: Wild card in regular expression
by moritz (Cardinal) on Nov 28, 2012 at 21:09 UTC
Re: Wild card in regular expression
by toolic (Bishop) on Nov 28, 2012 at 21:14 UTC

    Tip #9 from the Basic debugging checklist : YAPE::Regex::Explain

    The regular expression: (?-imsx:(ther*)) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- the 'the' ---------------------------------------------------------------------- r* 'r' (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

    Use . instead. Also, with different delimiters, you can clean up the replacement string:

    use warnings; use strict; my $test = "Here is the there"; $test =~ s{(ther.)}{<test>$1</test>}ig; print "$test\n"; __END__ Here is the <test>there</test>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1006098]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-03-29 09:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found