RegEx challenge - jrs

J

John R

I'm puzzled on how to make the following regex work:

String(1):
dsr:"Some text"; height:67; width:10; client:"seven"
String(2):
dsr:"Some;text"; height:67; width:10; client:"seven"

RegEx applied:
(\w+):?([^;]+)?;\s?

String(1) parsed great, resulting in two arrays:
dsr "Some text"
height 67
width 10
client "seven"

However, string(2) will not parse correctly because it has an extra
";" between the quotes:

Can regex ignore ; if they are between quotes?
 
A

A. Sinan Unur

(e-mail address removed) (John R) wrote in @posting.google.com:
I'm puzzled on how to make the following regex work:

String(1):
dsr:"Some text"; height:67; width:10; client:"seven"
String(2):
dsr:"Some;text"; height:67; width:10; client:"seven"

Yeah whatever ... An almost identical question was posted and answered
withing the last couple of days. Doesn't anyone lurk and read the FAQ any
more?

perldoc -q inside

By the way:
RegEx applied:
(\w+):?([^;]+)?;\s?

String(1) parsed great, resulting in two arrays:

What do you mean "resulting in two arrays"?

use strict;
use warnings;

my @data;

while(<DATA>) {
if( /^dsr:(".+"); height:(\d+); width:(\d+); client:(".+")\s*$/ ) {
push @data, {
dsr => $1,
height => $2,
width => $3,
client => $4,
};
}
}

use Data::Dumper;
print Dumper \@data;


__DATA__
dsr:"Some text"; height:67; width:10; client:"seven"
dsr:"Some;text"; height:67; width:10; client:"seven"
 
J

John R

A. Sinan Unur said:
while(<DATA>) {
if( /^dsr:(".+"); height:(\d+); width:(\d+); client:(".+")\s*$/ ) {
push @data, {
dsr => $1,
height => $2,
width => $3,
client => $4,
};
}
}

Thanks for the effort, but this will not work either. Your regex is
pattern matching the specific words "dsr" "height" etc... Maybe I
should have mentioned this earlier, the name/value pairs cannot be
predicted. The expected format is:
name:value; name:value; name:"value blah"; name:"valu;e"
Yeah whatever ... An almost identical question was posted and answered
withing the last couple of days. Doesn't anyone lurk and read the FAQ any
more?

Give me some more credit here. I read the FAQ. This is a unique
situation.
What do you mean "resulting in two arrays"?

RegEx option /m like /(\w+):?([^;]+)?;\s?/m for multiple matches
you'll get an array foreach grouping () eachtime the pattern is
repeatedly matched.

Does anyone understand my question?

<snip>
 
A

A. Sinan Unur

(e-mail address removed) (John R) wrote in
Thanks for the effort, but this will not work either. Your regex is
pattern matching the specific words "dsr" "height" etc... Maybe I
should have mentioned this earlier, the name/value pairs cannot be
predicted. The expected format is:
name:value; name:value; name:"value blah"; name:"valu;e"

The quality of the help you get is in direct proportion to the amount of
effort you put into formulating your question. Your question contained an
incomplete description of the problem.

Please see http://www.catb.org/~esr/faqs/smart-questions.html
Give me some more credit here. I read the FAQ. This is a unique
situation.

Your situation is not unique. You have not read the FAQ.
What do you mean "resulting in two arrays"?

RegEx option /m like /(\w+):?([^;]+)?;\s?/m for multiple matches
you'll get an array foreach grouping () eachtime the pattern is
repeatedly matched.

Each time you capture matches, the match in list context returns a list
of matches. There are no arrays involved. Again, it is time to hit the
FAQ list.
Does anyone understand my question?

Oh yes. And your question has been answered completely by the FAQ
reference I posted.

Sinan
 
T

Tad McClellan

RegEx option /m like /(\w+):?([^;]+)?;\s?/m for multiple matches
you'll get an array foreach grouping () eachtime the pattern is
repeatedly matched.


m//m has absolutely nothing to do with multiple matches.

m//m changes the meaning for only the ^ and $ anchor,
it *does nothing* for your pattern above because your
pattern does not contain ^ or $ anchors.

m// in *list context* returns a list of all the matching memories.

Does anyone understand my question?


It sounds just like this question:

How can I split a [character] delimited string except when inside
[character]? (Comma-separated files)

Do you understand the answer given along with it?
 
G

gnari

John R said:
"A. Sinan Unur" <[email protected]> wrote in message
[snip nice solution based on original description]

Thanks for the effort, but this will not work either. Your regex is
pattern matching the specific words "dsr" "height" etc... Maybe I
should have mentioned this earlier, the name/value pairs cannot be
predicted. The expected format is:
name:value; name:value; name:"value blah"; name:"valu;e"

[snip]

RegEx option /m like /(\w+):?([^;]+)?;\s?/m for multiple matches

actually, your regex seems to imply more that your 'expected format' above.
is the colon optional ?
is the value itself optional ?
doesn't the last name-value pair require a terminating semicolon ?
Does anyone understand my question?

don't be so arrogant when you are asking for help.

maybe you want something like: /(\w+):("[^"]+"|[^;]+);\s?/m
but in any case read the faq entry you have been referred to.

gnari
 
A

A. Sinan Unur

John R. said:
Tad said:
It sounds just like this question:

How can I split a [character] delimited string except when
inside [character]? (Comma-separated files)

Do you understand the answer given along with it?

Yes, this is much closer to my question.

This is the exact same FAQ entry I directed you to in my first response.

....
Again, regex is used an a lot of langs, so I'm just after the
expression itself.

Do you have a Perl question or not?

Feel free to study the source code of the modules mentioned in the FAQ
answer to see how it is done.

Sinan.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top