Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Wild card in regular expression

by sathya_myl (Acolyte)
on Nov 28, 2012 at 20:59 UTC( #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 (Archbishop) 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).


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?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2016-08-29 21:39 GMT
Find Nodes?
    Voting Booth?
    The best thing I ever won in a lottery was:

    Results (409 votes). Check out past polls.