I have a string that I need to parse. It meets the following requirements:
- It consists of 0 or more key-> value pairs.
- The key is always two letters.
- A value is one or more numbers.
- There will be no space between the key and value.
- There may or may not be a gap between individual pairs.
Examples of lines that I can see:
- AB1234 // One key-> value pair (Key = AB, Value = 1234)
- AB1234 BC2345 // Two key-> value pairs, separated by a space
- AB1234BC2345 // Two key-> value pairs not separated by space
- // Empty Sting, No key-> values ββpairs
- AB12345601BC1234CD1232PE2343 // Many pairs of key-> value, space
- AB12345601 BC1234 CD1232 PE2343 // Many key-> value pairs with spaces
I need to build a Perl hash of this line. If I could guarantee that it was 1 pair, I would do something like this:
$string =~ /([AZ][AZ])([0-9]+)/ $key = $1 $value = $2 $hash{$key} = $value
For several lines, I can potentially do something where, after each match of the above expression, I take a substring of the original string (excluding the first match), and then search again. However, I'm sure there is a smarter, perl-esque way to achieve this.
Desiring that I donβt have such a crappy data source that I could handle -
Jonathan
source share