Help with a Reg Ex

A

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.
 
M

Martijn Lievaart

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
 
A

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???

/
^ # 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.......
 
A

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???

/
^ # 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.......
 
J

Jürgen Exner

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
 
J

Jürgen Exner

Jürgen Exner said:
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
 
J

John W. Krahn

Jürgen Exner said:
Jürgen Exner said:
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

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
 
J

John W. Krahn

Jürgen Exner said:
Jürgen Exner said:
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

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

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



John
 
M

Martijn Lievaart

Jürgen Exner said:
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
 
M

Martijn Lievaart

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
 
J

Jürgen Exner

John W. Krahn said:
Jürgen Exner said:
[^\,] 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
 
J

John W. Krahn

Martijn said:
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
 
J

John W. Krahn

Jürgen Exner said:
John W. Krahn said:
Jürgen Exner said:
[^\,] 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.

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

perldoc perlop

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



John
 
T

Tad J McClellan

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?

:)
 

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

Similar Threads

perl reg expression help. 1
Reg Ex exclusion 5
Help with code plsss 0
Help with a function 8
Help me, going live with a quiz 0
Help 1
Reg Ex 6
Help with Loop 0

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top