Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Matching Newlines in RegExp

by stefan k (Curate)
on Jun 14, 2004 at 13:23 UTC ( #366529=perlquestion: print w/ replies, xml ) Need Help??
stefan k has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks,
thanks to your help (see Maybe Escape in Dynamic RegExps) I have been happy for quite while but now the fun is continued.
my $e = '\\' # kind of escape char my $s = '\''; # this is a string to use for splitting if(string_contains_newlines()) { $s .= "\n"; # ?? right so? } my $re = "(?<!\Q$e\E)\Q$s\E"; $split_re = qr/$re/s;
That is to say: when the string to split contains newlines (or --more precisely-- $/) I need to split the string according to a new regular expression that takes the former separator (stored in $s here) plus the newline character(s).

As you might guess, the above code doesn't work and I've run out of ideas what else to try.

Any hints or help appreciated.

Regards... Stefan
you begin bashing the string with a +42 regexp of confusion

Comment on Matching Newlines in RegExp
Select or Download Code
Re: Matching Newlines in RegExp
by Not_a_Number (Parson) on Jun 14, 2004 at 19:37 UTC

    First off, sorry, I have no idea what you're trying to do, so I can offer no concrete help.

    However, as nobody has answered this question in the more than six hours that have passed since it was posted, and as you have "run out of ideas", why not try YAPE::Regex::Explain?

    Something like this:

    use YAPE::Regex::Explain; # Your code as posted my $regex = YAPE::Regex::Explain->new ( qr/$split_re/ ); print $regex->explain;

    dave

Re: Matching Newlines in RegExp
by markjugg (Curate) on Jun 14, 2004 at 19:43 UTC
    When you say "the above code doesn't work", I think you mean "I didn't get the result I expected".

    So what result did you expect, and what result did you get?

    Sometimes I find it usefult to make simple "Test::More" style tests when illustrating for others that something isn't working. This means that I have a complete code sample that isolates and reproduces the issue. It also makes it clearer what results I expected compared to those I actually got. Finally, I can keep the result asan automated test script to run later to verify the quality of my software.

    I think with a clearer example, more suggestions will appear about how to fix or improve your code.

Re: Matching Newlines in RegExp
by orderthruchaos (Scribe) on Jun 14, 2004 at 19:50 UTC
    I agree with the prior replies... I'm not exactly sure what you are looking for... I tried out your code on a fairly simple example and it seems to work fine:
    #!/usr/bin/perl -w use strict; my $e = '\\'; # kind of escape char my $s = '\''; # this is a string to use for splitting my $str = "this \n contains \\'\n new lines " . "and single'\nquotes'\n more than\' once"; if( $str =~ m#$/# ) { $s .= "$/"; # ?? right so? } my $re = qr/(?<!\Q$e\E)\Q$s\E/; my @parts = split $re, $str; print scalar(@parts), "\n"; for ( my $i = 0 ; $i < @parts ; $i++ ) { print "$i: $parts[$i]\n"; }
    gives the following output:
    % ./test.pl 3 0: this contains \' new lines and single 1: quotes 2: more than' once
    The string was split on '\n, but not \'\n, \n or '.
Re: Matching Newlines in RegExp
by stefan k (Curate) on Jun 15, 2004 at 09:20 UTC
    Fellow Monks,
    thanks for you replies. I am well aware that I was in no state of mind to either hunt bugs or post questions.

    When later I broke the problem at hand down to a small test programm I found that my first guess actually worked (that making me feel less stupid :-).

    The real problem was another regexp from another module from a quite largy (self written) library. Anyway, today, with a fresh mind, I could solve this.

    Again: thanks.

    Regards... Stefan
    you begin bashing the string with a +42 regexp of confusion

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2014-09-18 22:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (126 votes), past polls