in reply to Repeating a capture group pattern within a pattern
If I were doing this for real I would use /g as Corion suggests. However, for interest's sake, here is one way to construct the pattern without repetition in the code.
#!/usr/bin/env perl use strict; use warnings; my $x = "0.01 NaN 2.30 4.44"; my $r1 = join '\s+', ('([Na0-9\.\-\+]+)') x 4; print "r1 is '$r1'\n"; my ($d, $e, $f, $g) = ($x =~ m/$r1/); print qq($d, $e, $f, $g\n);
Note that you don't need all those backslashes so the inner character class can be shortened to just [Na0-9.+-] but it has no effect on the end result.
If this is an XY Problem, then perhaps if you explain what you are actually trying to do someone could suggest a better approach.
🦛
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Repeating a capture group pattern within a pattern
by mldvx4 (Hermit) on Jul 15, 2024 at 10:10 UTC | |
"If this is an XY Problem, then perhaps if you explain what you are actually trying to do someone could suggest a better approach." Thanks. I am figuring out a way to parse the Finnish Meteorological Institute's abomination of a data file. It is like 10 pages of empty XML tags followed by gems like these:
I've written them several times over the years and they have not fixed their data nor deigned to even reply. | [reply] [d/l] |
by hippo (Archbishop) on Jul 15, 2024 at 11:28 UTC | |
That is pretty horrible but at least it appears well-formed whereby it is a space-separated list of decimal numbers and NaNs. I would probably just split it and go from there, TBH.
If you really only want the first four, then split / /, $str, 5 will bundle all the stuff you don't want into the unused 5th list item. HTH. 🦛 | [reply] [d/l] [select] |
by mldvx4 (Hermit) on Jul 15, 2024 at 11:43 UTC | |
Thanks. Is there a way to use split() on blocks of 21 items at a time? The readings come in blocks of 21 data points with the data points being determined by position in the sequence: Pressure, Geop Height, Temperature, Dew Point, Humidity, Wind Direction, Wind Speed MS, Wind UMS, Wind VMS, Precipitation Amount, Total Cloud Cover, Low Cloud Cover, Medium Cloud Cover, High Cloud Cover, Radiation Global, Radiation Global Accumulation, Radiation Net Surface LW Accumulation, Radiation Net Surface SW Accumulation, Radiation SW Accumulation, Visibility, and Wind Gusts. | [reply] [d/l] |
by tybalt89 (Monsignor) on Jul 15, 2024 at 18:04 UTC | |
by Corion (Patriarch) on Jul 15, 2024 at 11:49 UTC | |
by talexb (Chancellor) on Jul 16, 2024 at 00:29 UTC | |