Help with a Reg Ex

Help with a Reg Ex

am 09.01.2008 20:00:18 von amerar

I have the following expression in my Perl script:

if (/^"([^\,].+)"\,"$/)

Although I know that it is checking to see if the string starts with
some characters, it is not working for my input file.

Can someone explain to me in english what it is doing???

Thanks in advance.

Re: Help with a Reg Ex

am 09.01.2008 20:39:36 von Martijn Lievaart

On Wed, 09 Jan 2008 11:00:18 -0800, amerar@iwc.net wrote:

> I have the following expression in my Perl script:
>
> if (/^"([^\,].+)"\,"$/)
>
> Although I know that it is checking to see if the string starts with
> some characters, it is not working for my input file.
>
> Can someone explain to me in english what it is doing???

/
^ # beginning of string
" # match a quote
( # start capturing
[^\,] # one of any character, except comma
.+ # one of more of any character, except newline
) # stop capturing
" # another quote
\, # a comma
" # yet another quote
$ # end of string
/x

In other words, any string that starts with a quote; then two or more
characters, but not starting with a comma; a quote; a comma and a quote.

What do you want to achieve?

M4

Re: Help with a Reg Ex

am 09.01.2008 21:02:40 von amerar

On Jan 9, 2:39 pm, Martijn Lievaart wrote:
> On Wed, 09 Jan 2008 11:00:18 -0800, ame...@iwc.net wrote:
> > I have the following expression in my Perl script:
>
> > if (/^"([^\,].+)"\,"$/)
>
> > Although I know that it is checking to see if the string starts with
> > some characters, it is not working for my input file.
>
> > Can someone explain to me in english what it is doing???
>
> /
> ^ # beginning of string
> " # match a quote
> ( # start capturing
> [^\,] # one of any character, except comma
> .+ # one of more of any character, except newline
> ) # stop capturing
> " # another quote
> \, # a comma
> " # yet another quote
> $ # end of string
> /x
>
> In other words, any string that starts with a quote; then two or more
> characters, but not starting with a comma; a quote; a comma and a quote.
>
> What do you want to achieve?
>
> M4

Basically I have an input file, and the result of that IF statement
decides if I process the line read of not.......

Re: Help with a Reg Ex

am 09.01.2008 21:06:35 von amerar

On Jan 9, 2:39 pm, Martijn Lievaart wrote:
> On Wed, 09 Jan 2008 11:00:18 -0800, ame...@iwc.net wrote:
> > I have the following expression in my Perl script:
>
> > if (/^"([^\,].+)"\,"$/)
>
> > Although I know that it is checking to see if the string starts with
> > some characters, it is not working for my input file.
>
> > Can someone explain to me in english what it is doing???
>
> /
> ^ # beginning of string
> " # match a quote
> ( # start capturing
> [^\,] # one of any character, except comma
> .+ # one of more of any character, except newline
> ) # stop capturing
> " # another quote
> \, # a comma
> " # yet another quote
> $ # end of string
> /x
>
> In other words, any string that starts with a quote; then two or more
> characters, but not starting with a comma; a quote; a comma and a quote.
>
> What do you want to achieve?
>
> M4

I might add, the input file looks like this:

"#ABC","
[
","ABC Dispensing designs, manufactures and services dispensing
systems that utilize standard micro-processing technology and
proprietary operating software.","
]
"
"#ABE","
[
","Aber Diamond Corporation is a specialist diamond company focusing
on the mining and retail segments of the diamond industry. The Company
supplies rough diamonds to the global market through its forty percent
ownership in the Diavik Diamond Mine and owns one of the world's
premier retailers of diamond jewelry, Harry Winston.","
]
"

And I guess it is deciding if the line needs to be processed.......

Re: Help with a Reg Ex

am 09.01.2008 21:15:39 von jurgenex

"amerar@iwc.net" wrote:
>
>I have the following expression in my Perl script:
>
>if (/^"([^\,].+)"\,"$/)
>
>Although I know that it is checking to see if the string starts with
>some characters, it is not working for my input file.
>
>Can someone explain to me in english what it is doing???

Match a string that
^ starts with
" a double quote
( and then a group that consists of
[^\,] one of the three characters caret, backslash, comma
..+ followed by at least one more character
)
"\," and has the character sequence double quote, comma, doublequote
$ at the end of the string.

I hope I got that right.

jue

Re: Help with a Reg Ex

am 09.01.2008 21:40:49 von jurgenex

Jürgen Exner wrote:
>"amerar@iwc.net" wrote:
>>
>>I have the following expression in my Perl script:
>>
>>if (/^"([^\,].+)"\,"$/)
>>
>>Although I know that it is checking to see if the string starts with
>>some characters, it is not working for my input file.
>>
>>Can someone explain to me in english what it is doing???
>
>Match a string that
>^ starts with
>" a double quote
>( and then a group that consists of
>[^\,] one of the three characters caret, backslash, comma

Ooops, correction. Make that
[^\,] any single character, that is not backslash or comma

>.+ followed by at least one more character
>)
>"\," and has the character sequence double quote, comma, doublequote
>$ at the end of the string.
>
>I hope I got that right.
>
>jue

Re: Help with a Reg Ex

am 09.01.2008 22:14:02 von someone

Jürgen Exner wrote:
> Jürgen Exner wrote:
>> "amerar@iwc.net" wrote:
>>> I have the following expression in my Perl script:
>>>
>>> if (/^"([^\,].+)"\,"$/)
>>>
>>> Although I know that it is checking to see if the string starts with
>>> some characters, it is not working for my input file.
>>>
>>> Can someone explain to me in english what it is doing???
>> Match a string that
>> ^ starts with=20
>> " a double quote
>> ( and then a group that consists of=20
>> [^\,] one of the three characters caret, backslash, comma
>=20
> Ooops, correction. Make that
> [^\,] any single character, that is not backslash or comma

You were right the first time. A backslash before a normal character in =

a double quoted string is superfluous.

$ perl -le'print qr/[^\,]/'
(?-xism:[^,])



John
--=20
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Re: Help with a Reg Ex

am 09.01.2008 22:17:09 von someone

Jürgen Exner wrote:
> Jürgen Exner wrote:
>> "amerar@iwc.net" wrote:
>>> I have the following expression in my Perl script:
>>>
>>> if (/^"([^\,].+)"\,"$/)
>>>
>>> Although I know that it is checking to see if the string starts with
>>> some characters, it is not working for my input file.
>>>
>>> Can someone explain to me in english what it is doing???
>> Match a string that
>> ^ starts with=20
>> " a double quote
>> ( and then a group that consists of=20
>> [^\,] one of the three characters caret, backslash, comma
>=20
> Ooops, correction. Make that
> [^\,] any single character, that is not backslash or comma

A backslash before a normal character in a double quoted string is=20
superfluous.

$ perl -le'print qr/[^\,]/'
(?-xism:[^,])



John
--=20
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Re: Help with a Reg Ex

am 09.01.2008 22:24:14 von Martijn Lievaart

On Wed, 09 Jan 2008 20:40:49 +0000, Jürgen Exner wrote:

> Jürgen Exner wrote:
>>"amerar@iwc.net" wrote:
>>>
>>>I have the following expression in my Perl script:
>>>
>>>if (/^"([^\,].+)"\,"$/)
>>>
>>>Although I know that it is checking to see if the string starts with
>>>some characters, it is not working for my input file.
>>>
>>>Can someone explain to me in english what it is doing???
>>
>>Match a string that
>>^ starts with
>>" a double quote
>>( and then a group that consists of
>>[^\,] one of the three characters caret, backslash,
comma
>
> Ooops, correction. Make that
> [^\,] any single character, that is not backslash or
comma

No, is not comma:

[martijn@dexter ~]$ perl -e 'print "yes\n" if "," =~ /[^\,]/'
[martijn@dexter ~]$ perl -e 'print "yes\n" if "\\" =~ /[^\,]/'
yes
[martijn@dexter ~]$

HTH,
M4

Re: Help with a Reg Ex

am 09.01.2008 22:29:47 von Martijn Lievaart

On Wed, 09 Jan 2008 12:06:35 -0800, amerar@iwc.net wrote:

> On Jan 9, 2:39 pm, Martijn Lievaart wrote:
>> On Wed, 09 Jan 2008 11:00:18 -0800, ame...@iwc.net wrote:
>> > I have the following expression in my Perl script:
>>
>> > if (/^"([^\,].+)"\,"$/)

(snip)

> I might add, the input file looks like this:
>
> "#ABC","
> [
> ","ABC Dispensing designs, manufactures and services dispensing systems
(snip)

Yes, that regex will match the first line.

$ perl -e 'print "yes\n" if "\"#ABC\",\"" =~ /^"([^\,].+)"\,"$/'
yes

(Note extra backslashes in the input line because this is executed from
the commandline)

BTW, the backslashes before the commas are completely unneeded.

$ perl -e 'print "yes\n" if "\"#ABC\",\"" =~ /^"([^,].+)","$/'
yes

HTH,
M4

Re: Help with a Reg Ex

am 09.01.2008 22:42:22 von jurgenex

"John W. Krahn" wrote:
>Jürgen Exner wrote:
>> Jürgen Exner wrote:

>>> [^\,] one of the three characters caret, backslash, comma
>>
>> Ooops, correction. Make that
>> [^\,] any single character, that is not backslash or comma
>
>You were right the first time. A backslash before a normal character in
>a double quoted string is superfluous.

Yes, but:
- this is not a double quoted string but a RE character class. So the
backslash would be taken literally ARAIR.
- My mistake was to not consider that ^ at the beginning of a character
class indicates the negation of that class.

jue

Re: Help with a Reg Ex

am 09.01.2008 22:45:18 von someone

Martijn Lievaart wrote:
> On Wed, 09 Jan 2008 12:06:35 -0800, amerar@iwc.net wrote:
>
>> On Jan 9, 2:39 pm, Martijn Lievaart wrote:
>>> On Wed, 09 Jan 2008 11:00:18 -0800, ame...@iwc.net wrote:
>>>> I have the following expression in my Perl script:
>>>> if (/^"([^\,].+)"\,"$/)
>
> (snip)
>
>> I might add, the input file looks like this:
>>
>> "#ABC","
>> [
>> ","ABC Dispensing designs, manufactures and services dispensing systems
> (snip)
>
> Yes, that regex will match the first line.
>
> $ perl -e 'print "yes\n" if "\"#ABC\",\"" =~ /^"([^\,].+)"\,"$/'
> yes
>
> (Note extra backslashes in the input line because this is executed from
> the commandline)
>
> BTW, the backslashes before the commas are completely unneeded.
>
> $ perl -e 'print "yes\n" if "\"#ABC\",\"" =~ /^"([^,].+)","$/'
> yes

Actually, none of the backslashes are really needed:

perl -e 'print qq/yes\n/ if q/"#ABC","/ =~ /^"([^,].+)","$/'



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Re: Help with a Reg Ex

am 09.01.2008 23:00:28 von someone

Jürgen Exner wrote:
> "John W. Krahn" wrote:
>> Jürgen Exner wrote:
>>> Jürgen Exner wrote:
>=20
>>>> [^\,] one of the three characters caret, backslash, comma
>>> Ooops, correction. Make that
>>> [^\,] any single character, that is not backslash or comma
>> You were right the first time. A backslash before a normal character =
in=20
>> a double quoted string is superfluous.
>=20
> Yes, but:
> - this is not a double quoted string but a RE character class. So the
> backslash would be taken literally ARAIR.

Unless the regular expression uses single quote delimiters the pattern=20
is first interpolated as if it were a double quoted string.

perldoc perlop

Read through the sections "Quote and Quote-like Operators", "Regexp=20
Quote-Like Operators" and "Gory details of parsing quoted constructs".



John
--=20
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Re: Help with a Reg Ex

am 10.01.2008 04:18:10 von Tad J McClellan

amerar@iwc.net wrote:
>
> I have the following expression in my Perl script:
^^
^^
> if (/^"([^\,].+)"\,"$/)
>
> Although I know that it is checking to see if the string starts with
> some characters, it is not working for my input file.
>
> Can someone explain to me in english what it is doing???


Since you wrote it, you should be able to do that yourself.

Or did you mean "in someone else's Perl script" instead?

:-)


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"