I have a string where different predefined keywords introduce different data. Is there a way to do that using clever use of regexp, or something? Here is an example:
Keywords can be "first name: "
and "last name: "
. Now I want to parse:
"character first name: Han last name: Solo"
into
{ "first name: " => "Han ", "last name: " => "Solo" }
Of course, the order of the keywords in the input string is not fixed. This should also work on :
"character last name: Solo first name: Han"
I understand there are issues to be raised with spaces and so on. I'll ignore them here.
I know how to solve this problem looping on the different keywords, but I don't find that very pretty.
Split almost fits the bill. Its only problem is that it returns an array and not a hash, so I don't know which is the first name or the last name.
My example is somewhat misleading. Here is another one:
my @keywords = ("marker 1", "marker 2", "marker 3");
my $rawString = "beginning marker 1 one un marker 2 two deux marker 3 three trois and the rest";
my %result;
# <grind result>
print Dumper(\%result);
will print:
$VAR1 = {
'marker 2' => ' two deux ',
'marker 3' => ' three trois and the rest',
'marker 1' => ' one un '
};