match a long string in Regex..

A

Alont

original string:(it's one line):
javascript:if(confirm('http://validator.w3.org/ \n\nThis file was not
retrieved by Teleport Pro, because it is addressed on a domain or path
outside the boundaries set for its Starting Address. \n\nDo you want
to open it from the server?'))window.location='

because 'http://validator.w3.org/' is variable, so I wrote:

javascript:if\(confirm\('(.*|(\\n)*) \\n\\nThis file was not
retrieved by Teleport Pro, because it is addressed on a domain or path
outside the boundaries set for its Starting Address\. \\n\\nDo you
want to open it from the server?'))window.location='

I have tried/change again and again,
but it can't match, why?

this is all the code:

sub deleteTrash {
my $original = "javascript:if\(confirm\('(.*|(\\n)*) \\n\\nThis file
was not retrieved by Teleport Pro, because it is addressed on a domain
or path outside the boundaries set for its Starting Address\.
\\n\\nDo you want to open it from the
server\?'\)\)window\.location='";
my $body = shift;
if($body =~ s/$original//g)
{
return 0;
}
else
{
return $body;
}
}
 
P

Paul Lalli

Alont said:
I have tried/change again and again,
but it can't match, why?

this is all the code:

sub deleteTrash {
my $original = "javascript:if\(confirm\('(.*|(\\n)*) \\n\\nThis file
was not retrieved by Teleport Pro, because it is addressed on a domain
or path outside the boundaries set for its Starting Address\.
\\n\\nDo you want to open it from the
server\?'\)\)window\.location='";

The four embedded newlines hear actually need to be doubly escaped.
That is, the string should contain: \\\\n\\\\n rather than \\n\\n. This
is because pattern matching passes through two sets of interpolation.
First for double-quotish interpolation, then for regular expression
interpolation.

Additionally, because you are using double quotes to create $original,
\( gets treated as an actual parentheses, which is then interpolated by
the RegExp engine to mean "start capture". I would suggest changing to
single-quotes here:
my $original = q[javascript:if\(confirm\('(.*|\\n*) \\\\n\\\\nThis file
was not retrieved by Teleport Pro, because it is addressed on a domain
or path outside the boundaries set for its Starting Address\.
\\\\n\\\\nDo you want to open it from the
server\?'\)\)window\.location='];
#all the above on one line

One more thing - I don't think you're doing what you think you're doing
with (.*|\n*). That will match any sequence of non-newlines, or any
sequence of newlines. It will not match any sequence of characters
which contains newlines. For that, stick with .* and add the /s switch
to the pattern match.

Paul Lalli
 
A

Alont

Paul Lalli said:
The four embedded newlines hear actually need to be doubly escaped.
That is, the string should contain: \\\\n\\\\n rather than \\n\\n. This
is because pattern matching passes through two sets of interpolation.
First for double-quotish interpolation, then for regular expression
interpolation.

Additionally, because you are using double quotes to create $original,
\( gets treated as an actual parentheses, which is then interpolated by
the RegExp engine to mean "start capture". I would suggest changing to
single-quotes here:
my $original = q[javascript:if\(confirm\('(.*|\\n*) \\\\n\\\\nThis file
was not retrieved by Teleport Pro, because it is addressed on a domain
or path outside the boundaries set for its Starting Address\.
\\\\n\\\\nDo you want to open it from the
server\?'\)\)window\.location='];
#all the above on one line

One more thing - I don't think you're doing what you think you're doing
with (.*|\n*). That will match any sequence of non-newlines, or any
sequence of newlines. It will not match any sequence of characters
which contains newlines. For that, stick with .* and add the /s switch
to the pattern match.

Paul Lalli

now it works, thank you :)
 
B

Billy N. Patton

Alont said:
original string:(it's one line):
javascript:if(confirm('http://validator.w3.org/ \n\nThis file was not
retrieved by Teleport Pro, because it is addressed on a domain or path
outside the boundaries set for its Starting Address. \n\nDo you want
to open it from the server?'))window.location='

because 'http://validator.w3.org/' is variable, so I wrote:

javascript:if\(confirm\('(.*|(\\n)*) \\n\\nThis file was not
retrieved by Teleport Pro, because it is addressed on a domain or path
outside the boundaries set for its Starting Address\. \\n\\nDo you
want to open it from the server?'))window.location='

I have tried/change again and again,
but it can't match, why?

this is all the code:

sub deleteTrash {
my $original = "javascript:if\(confirm\('(.*|(\\n)*) \\n\\nThis file
was not retrieved by Teleport Pro, because it is addressed on a domain
or path outside the boundaries set for its Starting Address\.
\\n\\nDo you want to open it from the
server\?'\)\)window\.location='";
my $body = shift;
if($body =~ s/$original//g)
{
return 0;
}
else
{
return $body;
}
}

A day late and a dollar short

FYI
What I've found to help me GREATLY with large complex regexp is to use
the x option. It makes reading MUCH easier.

instead of having
$line =~ /^\s*(some_regx) (another_regex) .../; you get hte picture

I like
$line =~ /^\s*
(some_regex)\s+ # looking for this
(another_regex)\s+ # looking for that
...
/x;

I've have if/then/else where regex fill one complete page
Without the x option this would be almost impossible to write or debug.
What the x does is
1. ignore white space
2. ignore comments
3. ignore <cr>

For big harry regexp's use the x option

--
___ _ ____ ___ __ __
/ _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
/ _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodlogy Group
Dallas, Texas, 214-480-4455, (e-mail address removed)
 
T

Tad McClellan

Billy N. Patton said:
What I've found to help me GREATLY with large complex regexp is to use
the x option. It makes reading MUCH easier.


Which is why it will be "on" by default in Perl 6.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top