Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Small replace regular exp help...pls

by dani_cv_74 (Novice)
on Mar 09, 2006 at 21:59 UTC ( [id://535528]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Small replace regular exp help...pls
by polettix (Vicar) on Mar 09, 2006 at 22:46 UTC
    It's usually better to put what you've tried so far: it demonstrates good will on your side, and helps others to understand what you're after. How (Not) To Ask A Question is a good reading in this case -- your question resembles some homework. Otherwise, you have to be prepared to answers like this
    $str = 'Data';
    that make me feel smart but that actually waste your time, my time and (unluckily for my XP), other monks' time.

    Back to business, answers before this post concentrated in isolating all that's after the colon, more or less, but I suspect that you actually want to get rid of both 'Some text1 : ' and ' Some Text2'. The final word is up to you, so: what do you need exactly, and what have you tried so far?

    Update: it seems that matze answered just before I was posting my comment, with a solution pointing towards my interpretation of your question. Good for you?

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: Small replace regular exp help...pls
by SamCG (Hermit) on Mar 09, 2006 at 22:29 UTC


    You might also try, especially if "Some Text 1:" isn't standardized and you don't actually want the word "Data":
    $str= "Some text1 : Data Some Text2"; $str=~/:\s+Data\s(.+)\z/; $youwant=$1; ##$1 is the captured (.+) part print $youwant;
    prints "Some Text2"

    It's not technically a substitution, of course -- $str remains the same -- but if you wanted to you could just set $str to $youwant at the end (or even to $1 earlier). or you could:
    $str= "Some text1 : Data Some Text2"; $str=~s/.+?:\s+Data\s(.+)\z/$1/; print $str;
    to use the substitution operator.. .

    update: I thought the "Some Text2" WAS the data, and the "Data" was a literal (I don't know, announcing that a bunch of data was to follow, or something. Dense of me, I guess). My suggestions could be modified, but it would depend on what "Data" contained.

    update2: dani_cv_72 looks quite a bit like dani_cv_perl, who posed very similar questions.
Re: Small replace regular exp help...pls
by CountZero (Bishop) on Mar 09, 2006 at 22:24 UTC
    Or use split which takes a regular expression as an argument.

    my (undef, $data) = split / : /, $str; print "Data: $data\n";

    CountZero

    PS: Honestly, tell us, this is homework, isn't it?

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Small replace regular exp help...pls
by GrandFather (Saint) on Mar 09, 2006 at 22:08 UTC
    my $str = "Some text1 : Data Some Text2"; $str =~ s/^Some text1 : //; print $str;

    Prints:

    Data Some Text2

    DWIM is Perl's answer to Gödel
Re: Small replace regular exp help...pls
by matze (Friar) on Mar 09, 2006 at 22:34 UTC
    If you just want to extract the Data block, you could use:
    my $str = "Some text1 : Data Some Text2"; $str =~ s/Some text1 : (.*) Some Text2/$1/g; print "${str}\n";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-20 01:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found