Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

regex renaming

by ArifS (Beadle)
on Nov 03, 2014 at 19:20 UTC ( [id://1105936]=perlquestion: print w/replies, xml ) Need Help??

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

Her is the script I am trying-
use strict; use warnings; my $text; while (my $text = <DATA>) { # match a word that contain 'ou' or # if ($text =~ m/(ou)|(#)$/) { print $text; } } __DATA__ hello.world# world\tour Game.started OUTPUT hello.world# world\tour
I also would like to filter any line that matches any of the following & replace with a (dash) - :
\/:*?"<>|
eg:
hello.world#
world-tour

Please let me know.

Replies are listed 'Best First'.
Re: regex renaming
by toolic (Bishop) on Nov 03, 2014 at 19:30 UTC
    One way to replace is to use s///:
    use strict; use warnings; my $text; while ( my $text = <DATA> ) { # match a word that contain 'ou' or # if ( $text =~ m/(ou)|(#)$/ ) { $text =~ s{[/\\]}{-}g; print $text; } } __DATA__ hello.world# world\tour Game.started

    That replaces the \ and the / with -. You can add the rest.

    UPDATE: added alternate delimiters in response to OP private msg.

Re: regex renaming
by davido (Cardinal) on Nov 03, 2014 at 23:02 UTC

    Utter nonsense. This is not the question of a seeker of Perl wisdom. It seems, in fact, to be seeking a means of avoiding acquiring Perl wisdom.

    You have identified that you wish to do a substitution. You seem to be aware of regular expressions. So read the documentation, perlretut for example, and take a stab at it. The worst that can happen if you try is you make a mistake, and with a little effort, you'll learn from the mistake. The best that can happen if you don't try is you'll learn nothing and remain dependent on others to solve your problem for you.


    Dave


      Hi Dave & Toolic,
      My thoughts when i read this post were on the same lines. I could not bring myself even to put the effort to put the regex docu link in the post :D.
      On a serious note, i wanted to point out something i see in the documentation which could be hindering novice perl programmers from hitting the documentation up front.
      • Perl documentation seems highly contextual, and assumes prior knowledge of language concepts
        As an example i was trying to create a package yesterday to house some code id written. I tried reading the perldoc for perlmod and while it did capture a lot of relevant formation in a less than succinct manner, a large section of the documentation is devoted to symbol tables, an area ive never needed to tamper with in the last 6 months that ive started re-using perl. Also i dont understand symbol tables or their full usage, apart from its importance of storing the package variables and maybe its importance in identifying the namespace.
      • The documentation is disparate.
        I finally found what i was looking for what i needed to know and was not obvious to me i.e how to declare, how to export variables , quirks of @INC and finally how to import) on the perlmaven site in one page. Apart from a manpage like perlreftut or perlretut that u pointed, i am not aware of many places where the documentation is to the point and captures quickly the different aspects one needs to know.
        Perlguts i hesitate to even open(wouldnt it be better served if we had the actual language specification there for people to understand the grammar n whatnot),but i guess i have a long way to go before i deduce its meaningwisdom.
        Perlobj documentation refers to perlref in the first line of the text and perlref asks us to check perlrequick. For a new user,thats frustrating
        I guess the solution is to invest in a book (i have posted a question on the PM site for my specific needs Book Reccomendation for Web Development using Perl , and got awesome suggestions which i am acting on ) but no book will i think be able to replace man pages and perldocs for a lot of reasons, which are outside the context of this discussion.

      these are two things ive been dealing with this week and they jumped to mind when i saw Dave's post.
      My final analysis i wanted to share is regardless of the above, they are at best minor issues to a person who falls in love with the language or is dependent on the language for his their livelihood. So i hope my comments will be taken in the right spirit by all who read this post.

      The temporal difficulty with perl is u need to know C well to know the awesome.else u just keep *using* it and writing inefficient code
      "you'll learn from the mistake"

      i must be learning alot then :) hopefully i am learning the "right" way lol
Re: regex renaming
by perlron (Pilgrim) on Nov 03, 2014 at 19:31 UTC
    HI
    Two things one must do
    1. Read perldoc perlre documentation. Its all covered there.
    2. check existing perlmonks posts for similar solutions. I am very confident you will find many answers.
    If you still face a problem , look no further. ask away.

Log In?
Username:
Password:

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

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

    No recent polls found