Google Grupper støtter ikke lenger nye Usenet-innlegg eller -abonnementer. Historisk innhold er fortsatt synlig.

Regex: Can I match newlines like with Perl's s flag? (If not, will this be in JS2?)

Sett 1 gang
Hopp til første uleste melding

jos...@my-deja.com

ulest,
13. mai 2000, 03:00:0013.05.2000
til
Hi,
The docs I've read at developer.netscape.com say that . matches
everything but the newline character...is there any way to match it in
something like the following text? (If not, hopefully JS2 will have
this? :-) )

Using the JS standalone shell and Mozilla build from today on linux:

var re = /{:(.*?):}/g;

var str = "{:matches:}\
{:matches fine:}\
{:\
does not match :/\
:}\
{:and so does this:}\
{: but\
this\
wont:}\
{:boo:}\
";

function showfunc(match, txt)
{
print ("Found this: [" + txt + "]\n");
}

str.replace(re, showfunc);


I get this result:

Found this: [matches]

Found this: [matches fine]

Found this: [and so does this]

Found this: [boo]


Similarly, in Purl:

my $str = q[
{:matches:}
{:matches fine:}
{:
does not match :/
:}
{:and so does this:}
{: but
this
wont:}
{:boo:}
];

sub showfunc($) { my $txt = shift;
print "Found this: [" . $txt . "]\n";
}

$str =~ s/{:(.*?):}/showfunc($1)/gse;

With this output:


Found this: [matches]
Found this: [matches fine]
Found this: [
does not match :/
]
Found this: [and so does this]
Found this: [ but
this
wont]
Found this: [boo]


I guess the best I could do for now would be to remove newlines first or
replace them with something else. I have looked over some of the JS2
material, but can't tell so far whether this is planned for it. Any tips
greatly appreciated.

Thanks,
Josh


Sent via Deja.com http://www.deja.com/
Before you buy.

Roger Lawrence

ulest,
15. mai 2000, 03:00:0015.05.2000
til
Hi Josh,

I ran your test case (admittedly on NT, not Linux - i'll try to find a
build to make sure this isn't a platform bug) and everything is just fine:

Found this: [matches]

Found this: [matches fine]

Found this: [does not match :/]

Found this: [and so does this]

Found this: [ butthiswont]

Found this: [boo]


Note that the string value you're using doesn't contain any newline
characters, the '\' is being interpreted as a string literal continuation
and completely escapes the following newline. If you force newlines into the
string with :

var str = "{:matches:}\n{:matches fine:}\n{:\ndoes not match :/\n:}\n{:and
so does this:}\n{: but\nthis\nwont:}\n{:boo:}\n";

You'll get the behaviour you describe (so maybe this is what you meant?).
The issue is that '.' doesn't match a newline, no matter what. Perl has a
'/s' mode that allows '.' to do so, but we didn't implement it (I don't
remember the discussion about why not). In order to get what you need, try :

var re = /{:((.|\n)*?):}/g;

instead, which will match the embedded newlines. Friedl's book (the O'Reilly
Regular Expressions book) has a pretty good discussion of other
alternatives, too.

I'm not aware of any planned changes to the regular expression package for
JS2, but now would be a good time to lobby for them if you're so inclined.

-roger

0 nye meldinger