Ask a Question related to PERL Miscellaneous, Design and Development.
-
Kenneth Baltrinic #1
Regular Expression Problem
I am trying to parse a relatively simple SQL query with a regular
expression. All is going well except for one issue I don't seem to be able
to find a solution for. Handling optional parentheses and brackets. This
seems like a back reference problem to me but I am not sure. Let me give a
simple example.
An table name might be enclosed in brackets or it might not. For the sake of
simplification, lets assume that the pattern [A-Za-z_]+ is what we are
looking for in a table name. A really simple solution to find table names
that are bracket enclosed or not would as follows:
/(\[[A-Za-z_]+\])|([A-Za-z_]+)/
The problem is that the pattern for a table name is not in reality as simple
as [A-Za-z_]+ . In fact it is quite long, and repeating it twice in the
expression seems inefficient not to mention prone to bugs if I need to tweak
it and don't get each side exactly the same. What I want to do is something
like the following:
/(\[?)[A-Za-z_]+\1/
This almost works except of course that my backreference is looking for an
opening bracket [ when I need it to be looking for a closing bracket ]. So
here it the crux of my question. Is there any way to do something like
this--to have a backreference that does some sort of fuzzy match? I have a
similar issues with parentheses.
Thanks for the help,
Ken Baltrinic
Kenneth Baltrinic Guest
-
regular expression problem ? and * characters
Im writing a perl script now and this is part of the sricpt chomp = ($pattern = ARGV); for each(@thisarray) { if($_ =~ m/$pattern/i) {... -
Problem with regular expression
I'm trying to use a regular expression to remove from the output of a call to ToString with an XML object, the text between "<?" and "?>" txtvar... -
php regular expression problem
Hi, I've got just a small problem, it's probably not very complex but I'm not very experienced with regular expression stuff. I just need to... -
help on regular expression
Hi, I need some help on regular expression... i have following in variable $total_count $total_count = "##I USBP 000001 10:38:09(000)... -
Regular Expression HELP!
I'm stripping out the attributes in <TD> tags...but I want to strip out everything BUT the COLSPAN attribute. The following strips out all... -
Sam Holden #2
Re: Regular Expression Problem
On Wed, 02 Jul 2003 04:33:24 GMT,
Kenneth Baltrinic <kenneth@baltrinic.com> wrote:Why not something like:> I am trying to parse a relatively simple SQL query with a regular
> expression. All is going well except for one issue I don't seem to be able
> to find a solution for. Handling optional parentheses and brackets. This
> seems like a back reference problem to me but I am not sure. Let me give a
> simple example.
>
> An table name might be enclosed in brackets or it might not. For the sake of
> simplification, lets assume that the pattern [A-Za-z_]+ is what we are
> looking for in a table name. A really simple solution to find table names
> that are bracket enclosed or not would as follows:
>
> /(\[[A-Za-z_]+\])|([A-Za-z_]+)/
>
> The problem is that the pattern for a table name is not in reality as simple
> as [A-Za-z_]+ . In fact it is quite long, and repeating it twice in the
> expression seems inefficient not to mention prone to bugs if I need to tweak
> it and don't get each side exactly the same. What I want to do is something
> like the following:
(\[$table_name_pattern])|($table_name_pattern)
Of course if nested parentheses are needed things get more complicated, and
moving to a non-regex solution is often easier. Something like
Parse::RecDescent, for example.
Can't help with that, sorry...>
> /(\[?)[A-Za-z_]+\1/
>
> This almost works except of course that my backreference is looking for an
> opening bracket [ when I need it to be looking for a closing bracket ]. So
> here it the crux of my question. Is there any way to do something like
> this--to have a backreference that does some sort of fuzzy match? I have a
> similar issues with parentheses.
--
Sam Holden
Sam Holden Guest
-
Chauncey Williams #3
Re: Regular Expression Problem
> What I want to do is something like the following:
Maybe a (?<= [ )] positive lookbehind is what you are looking for...>
> /(\[?)[A-Za-z_]+\1/
>
> This almost works except of course that my backreference is looking
> for an opening bracket [ when I need it to be looking for a closing
> bracket ]. So here it the crux of my question. Is there any way to
> do something like this--to have a backreference that does some sort
> of fuzzy match? I have a similar issues with parentheses.
>
> Thanks for the help,
> Ken Baltrinic
I'm pretty new to this though, and if what you are showing is just a small
part of the regex, it may not work.
XC
Chauncey Williams Guest
-
Abigail #4
Re: Regular Expression Problem
Kenneth Baltrinic (kenneth@baltrinic.com) wrote on MMMDXCII September
MCMXCIII in <URL:news:ogtMa.8784$Hw.6276188@news2.news.adelphi a.net>:
$$ I am trying to parse a relatively simple SQL query with a regular
$$ expression. All is going well except for one issue I don't seem to be able
$$ to find a solution for. Handling optional parentheses and brackets. This
$$ seems like a back reference problem to me but I am not sure. Let me give a
$$ simple example.
$$
$$ An table name might be enclosed in brackets or it might not. For the sake of
$$ simplification, lets assume that the pattern [A-Za-z_]+ is what we are
$$ looking for in a table name. A really simple solution to find table names
$$ that are bracket enclosed or not would as follows:
$$
$$ /(\[[A-Za-z_]+\])|([A-Za-z_]+)/
$$
$$ The problem is that the pattern for a table name is not in reality as simple
$$ as [A-Za-z_]+ . In fact it is quite long, and repeating it twice in the
$$ expression seems inefficient not to mention prone to bugs if I need to tweak
$$ it and don't get each side exactly the same. What I want to do is something
$$ like the following:
$$
$$ /(\[?)[A-Za-z_]+\1/
$$
$$ This almost works except of course that my backreference is looking for an
$$ opening bracket [ when I need it to be looking for a closing bracket ]. So
$$ here it the crux of my question. Is there any way to do something like
$$ this--to have a backreference that does some sort of fuzzy match? I have a
$$ similar issues with parentheses.
Use the (??{ }) construct:
/(\[?)[A-Za-z_]+(??{$1 ? "]" : ""})/
Abigail
--
perl -MLWP::UserAgent -MHTML::TreeBuilder -MHTML::FormatText -wle'print +(
HTML::FormatText -> new -> format (HTML::TreeBuilder -> new -> parse (
LWP::UserAgent -> new -> request (HTTP::Request -> new ("GET",
"http://work.ucsd.edu:5141/cgi-bin/http_webster?isindex=perl")) -> content))
=~ /(.*\))[-\s]+Addition/s) [0]'
Abigail Guest
-
Krogman #5
Regular Expression Problem
Greatings
I'm hoping that someone could help me a regular expression string. I have a
javascript that checks a form text entry to verify that text entered meets a
pre-defined criteria. The problem is, I can't figure out the proper string to
use to check the form data entered. Please see code attached.
I'm trying to create an expression that only allows the following pattern:
numbers that range from 1 to 100 with an option astrick (*) after the number
like 78*
or the 2 letters 'in' (mixed case allowed, In, iN, IN)
I just can't seem to the get the pattern correct. Any help would be
appreciated. Thanks, JK
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>
<script language="JavaScript1.2">
function check(field)
{
pattern1 = /^[^0]$|^[1]+[0]+[0]\x2A?$|^[1-9]\x2A?$|^[1-9][0-9]\x2A?$/;
if(pattern1.test(field.value)==false)
{
alert("Please enter a number between 1-100 with an * after or IN. Changing
to 1.");
field.value = 1;
}
}
</script>
</head>
<body>
<form action="test.cfm" method="post">
<input type="text" name="mark" size="4" maxlength="4"
onblur="check(this);"><br>
<input type="text" name="foo" size="4" maxlength="4"><br>
<input type="submit" name="submit" value="Continue">
</form>
</body>
</html>
Krogman Guest
-
Krogman #6
Re: Regular Expression Problem
I put some more though into this and I have found the solution:
pattern1 =
/^[^\x21\x22\x23\x24\x24\x25\x26\x27\x28\x29\x2A\x2B\ x2C\x2D\x2E\x2Fa-hj-mo-zA-H
J-MO-Z]^[^0]*$|^[1]+[0]+[0]\x2A?$|^[1-9]\x2A?$|^[1-9][0-9]\x2A?$|^[n]$|^[N]$|^[n
]$|^[N]$/;
I have included a function to change the form value if it is above 100.
Thanks.
function check(field,limit)
{
pattern1 =
/^[^\x21\x22\x23\x24\x24\x25\x26\x27\x28\x29\x2A\x2B\ x2C\x2D\x2E\x2Fa-hj-mo-zA-H
J-MO-Z]^[^0]*$|^[1]+[0]+[0]\x2A?$|^[1-9]\x2A?$|^[1-9][0-9]\x2A?$|^[i][n]$|^[I][N
]$|^[I][n]$|^[i][N]$/;
if(pattern1.test(field.value)==false)
{
alert("Please enter a number between 1-100 with an * after or IN. Changing
to 1.");
field.value = 1;
}
else
{
if(field.value > limit)
{
alert("Please enter a number between 1-100 or IN. Changing to 1.");
field.value = 1;
}
}
}
Krogman Guest



Reply With Quote

