I'm trying to extract name:value pairs from a string similar to this one
{a:b,c:d,e:f}
The individual pieces are more complicated, but its similar in principal
Here is a test script I use
$f = "a:b,c:d,e:f";
@a = ($f =~ m/(?:(\w+):(\w+))(?:,(?:(\w+):(\w+)))+/);
print "@a\n";
I should be seeing "a b c d e f", but I am only getting "a b e f",
i.e. the first and last group matched. Why am I not seeing the rest?
Ideally I want to just assign the result of the match to a hash.
-- Boris 3 2121
Boris Pelakh wrote: Here is a test script I use
$f = "a:b,c:d,e:f"; @a = ($f =~ m/(?:(\w+):(\w+))(?:,(?:(\w+):(\w+)))+/); print "@a\n";
I should be seeing "a b c d e f", but I am only getting "a b e f",
What about a simple
use strict;
use warnings;
my $f = "a:b,c:d,e:f";
my @a;
for (split /,/, $f) {
push @a ,(split /:/);
}
print "@a\n";
In my opinion much easier to write and to read than a complicated RE.
jue
In article <44**************************@posting.google.com >, Boris
Pelakh <pe****@yahoo.com> wrote: I'm trying to extract name:value pairs from a string similar to this one
{a:b,c:d,e:f}
The individual pieces are more complicated, but its similar in principal
Here is a test script I use
$f = "a:b,c:d,e:f"; @a = ($f =~ m/(?:(\w+):(\w+))(?:,(?:(\w+):(\w+)))+/); print "@a\n";
I should be seeing "a b c d e f", but I am only getting "a b e f", i.e. the first and last group matched. Why am I not seeing the rest?
You only see "a b e f" in your output because you only have 4 capturing
parentheses in your pattern, and you only apply your pattern once. The
entire string is matched due to the '+' after the second subgrouping,
but the capturing bits in this group are applied twice: first to
capture the 'c' and the 'd', and then to match and capture the 'e' and
the 'f', overwriting the 'c' and 'd'.
If you want all of the matching bits, use a simpler pattern but use it
repeatedly with the 'g' modifier:
my @a = ( $f =~ m/(?:(\w+):(\w+))/g );
but, as Jürgen pointed out, using split twice is simpler.
Ideally I want to just assign the result of the match to a hash.
Well, then, assign the results of the match to a hash:
my %h = ( $f =~ m/(?:(\w+):(\w+))/g );
FYI: this newsgroup is defunct. Try comp.lang.perl.misc in the future.
Boris Pelakh wrote:
(snipped) I'm trying to extract name:value pairs
$f = "a:b,c:d,e:f"; @a = ($f =~ m/(?:(\w+):(\w+))(?:,(?:(\w+):(\w+)))+/);
I should be seeing "a b c d e f", but I am only getting "a b e f",
#!perl
$f = "a:b,c:d,e:f";
@a = split (/:|,/, $f);
print "@a";
Purl Gurl This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Han |
last post by:
Determining the pattern below has got my stumped.
I have a page of HTML and need to find all occurrences of the following
pattern:
score=9999999999&
The number shown can be 5-10 characters...
|
by: Sims |
last post by:
Hi,
I need some help to split data using regular expression
Consider the string
'1,2,3', I can split it using, preg_split("/,/", '1,2,3') and i correctly
get =1, =2,=3.
Now if i have
|
by: python_charmer2000 |
last post by:
I want to match several regexps against a large body of text. What I
have so far is similar to this:
re1 = <some regexp>
re2 = <some regexp>
re3 = <some regexp>
big_re = re.compile(re1 +...
|
by: iannorton |
last post by:
Hi,
I'm trying to write a RegExp that will return search an array and
return the closest 10 matches, for example, if i entered "Hel" and my
array contained "Hello", "Hell", it would return Hello...
|
by: Christoph |
last post by:
I'm trying to set up client side validation for a textarea form element to
ensure that the data entered does not exceed 200 characters. I'm using the
following code but it doesn't seem to be...
|
by: Licheng Fang |
last post by:
Basically, the problem is this:
'do'
Python's NFA regexp engine trys only the first option, and happily
rests on that. There's another example:
'oneself'
The Python regular expression...
|
by: Davros9 |
last post by:
Trying to get Regular Expressions working.......
----------------
Public Function SepString(InField As String) As String
''seperates on space and comma
Dim RE As New RegExp
Dim Matches As...
|
by: Matt |
last post by:
Hello all,
I have just discovered (the long way) that using a RegExp object with
the 'global' flag set produces inconsistent results when its test()
method is executed. I realize that 'global'...
|
by: matheussousuke |
last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails.
The goal is: Make it send the email with the URL...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |