Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Regex question

by IPstacks (Novice)
on Nov 14, 2001 at 22:34 UTC ( [id://125383]=perlquestion: print w/replies, xml ) Need Help??

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

I have a script that reads a file one line at a time and searches for "--foo" and print's everything after that. I have that part working fine, but, when it prints the output it has everything on the line after --foo
At the end of the line in the file is a TAB followed by a \
I Think that I need to use a regular expression to tell it to ignore the TAB \ but I'm not sure exactly how to word it as I'm new to this. Also, the output is in quoted (") text and I would like to strip off the quotes around the text.
Is regex what I want to use?

Replies are listed 'Best First'.
Re: Regex question
by monkfish (Pilgrim) on Nov 14, 2001 at 22:47 UTC
    If you don't want to print a line with a tab and a slash put this in your loop...

    print $_ unless ($_ =~ m@^\t\\$@);

    If you want to stop printing when you get to a line like that you could use:

    last if ($_ =~ m@^\t\\$@);

    Edit Changed / to \\

    -monkfish (The Fishy Monk)

Re: Regex question
by jeroenes (Priest) on Nov 14, 2001 at 23:28 UTC
    If the tab and the '\' are always at the end of a line, you can use the lvalue of substr:
    substr($line,-3,2)=''; #assuming your line ends with a newline $line =~ tr/"//d; #deletes all quotes in your text
    If you want to take care of nested quotes, take a look at Parse::RecDescent or the likes.

    Cheers,

    Jeroen

Re: Regex question
by c (Hermit) on Nov 14, 2001 at 23:37 UTC
    one other path you may want to head down is getting rid of your tab and following slash using the s/// operator:

    my $string = "hello\t\\"; $string =~ s/\t\\//g;

    this should return the value of $string as simply hello

    humbly -c

Re: Regex question
by atlantageek (Monk) on Nov 15, 2001 at 09:47 UTC
    Regex will work assumming that the content is in $line
    $line =~ /"(.*)\t\\"/;
    I always wanted to be somebody... I guess I should have been more specific.
      and also use $1 now for your value.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-04-16 07:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found