Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Complex Regex

by stallion (Acolyte)
on Jul 13, 2012 at 13:35 UTC ( [id://981620]=perlquestion: print w/replies, xml ) Need Help??

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

Hi guys is there are any regex in perl to convert three letter string into two letter for eg..

ABC - hi GHR - hi 1DF - hi uy6 - hi

All three letter words ( Small or Caps or no) is converted into hi (Two letter string)....Thanks Monks!!!!!!!

Replies are listed 'Best First'.
Re: Complex Regex
by toolic (Bishop) on Jul 13, 2012 at 13:42 UTC
    s///
    use warnings; use strict; while (<DATA>) { chomp; s/ ^ [0-9a-z]{3} $ /hi/xi; print "$_\n"; } __DATA__ ABC GHR 1DF uy6 fore

    See also:

      Wonderful toolic!!!!!!!!! it works great..thanks
Re: Complex Regex
by davido (Cardinal) on Jul 13, 2012 at 17:36 UTC

    s/\b\w{3}\b/hi/g would perform the substitution in a document where there could be multiple potential groups per string. If you just want to limit the match to strings that are exactly three characters long, it becomes simpler. s/\A\w{3}\z/hi/

    Try it out: The first version. Notice how the "List Context Return Values" captured by the example pattern exclude any substring that is more than three characters long.

    And the second version, where there can be only one case per string.

    perlrequick and perlretut would be helpful for you.


    Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 23:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found