in reply to parsing a line of text with inconsistent delimiters

A regexp something like the following should match each such "second number" in the line.
/ [A-Z]+ # WB \(\d+,\d+\) # (1,2) \s*=\s* # = (?: # start non-capturing group \d+\.\d+, # a float followed by comma \s*(\d+\.\d+),? # capture a float (followed by comma?) ) # end-group /gx

To change it use the substitution operator (perhaps with /e flag so that you can pass the matched number to a function to get back the value you want it to be changed to).

-David

Update: fixed a typo.
Update2: and another, thanks naikonta.

Replies are listed 'Best First'.
Re^2: parsing a line of text with inconsistent delimiters
by naikonta (Curate) on Sep 13, 2007 at 16:02 UTC
    Oops,
    Unmatched ) in regex; marked by <-- HERE in m/ [A-Z]+ # WB \(\d+,\d+) <-- HERE # (1,2) \s*=\s* # = (?: # start non-capturing group \d+\.\d+, # a float followed by comma \s*(\d+\.\d+),? # capture a float (followed by comma?) ) # end-group / at /tmp/wb line 10.
    Matt, you can do it also with split but you have to join the parts back together after changing the part you want.
    $_ = 'WB(1,2)= 0.000, 1.23, TB(1,2)= 0.0, 253.0, TMB(1,2)= 0. +0, 1.0, SL(1,2)= 0.00, 1.00'; my @parts = split /, /; $parts[1] = 3.21; # change 1.23 with 3.21 $_ = join ', ', @parts; print $_, "\n"; # output: # WB(1,2)= 0.000, 3.21, TB(1,2)= 0.0, 253.0, TMB(1,2)= 0.0, +1.0, SL(1,2)= 0.00, 1.00

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!