nabeenj has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks
I'm new to perlmonks and seeking your wisdom.
Here's my question...
I have a string such as this –
something in this string is a number. ${=NumberFormat "99999999"} is a number. And another ${=CharFormat "X*"} is ${=NumberFormat "9+"}.I want to match everything ahead of ${=CharFormat "X*"} up to the first occurrence of '${'. If this literal string '${' does not occur in the rest of the string, then match up to the end. So if, by mistake, the left curly bracket is missing from the last NumberFormat placeholder – i.e. if that placeholder has been written '$=NumberFormat "9+"}' – then match up to the end of the string.
Below is my test perl script that is matching the string between the CharFormat and NumberFormat placeholders, but if I remove the left curly bracket from the NumberFormat placeholder nothing matches.
my $string_exp = 'something in this string is a number. ${=NumberForma +t "99999999"} is a number. And another ${=CharFormat "X*"} is ${=Numb +erFormat "9+"}.'; # remove the left curly bracket from the last NumberFormat placeholder + in the above string my $placeholder = '${=CharFormat "X*"}'; print "$string_exp\n"; print "$placeholder\n"; if ( $string_exp =~ m/\Q$placeholder\E(.*)?(\$\{)|\z/ ) { print "\"$1\"\n"; } print "END\n";
I hope this makes some sense.
Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regex matching next occurrence or to the end of line.
by Eily (Monsignor) on Feb 04, 2015 at 16:54 UTC | |
by nabeenj (Initiate) on Feb 04, 2015 at 18:03 UTC | |
|
Re: Regex matching next occurrence or to the end of line.
by hdb (Monsignor) on Feb 04, 2015 at 18:21 UTC | |
by nabeenj (Initiate) on Feb 05, 2015 at 10:12 UTC |