[Israel.pm] (no subject)

Gaal Yahas gaal at forum2.org
Wed Feb 4 10:50:48 PST 2009


Is there a way to put a single quote inside a string in this language?
E.g. if your input has

ServiceGroup='Ron\'s Fancy Service'

Then your parsing will not work.



Assuming your input is well-formed, the way to do these things usually
is to "use a real parser", for example with Parse::RecDescent.
However, shotgun regexps can get you far if used judiciously.

Your approach is to destruct the string gradually is reasonable that
way. But you need to tweak the '(.*?)' parts to protect yourself from
escaped quote marks. Removing all \' won't work, because \\' can mean
"literal backslash and then real quote end". So you need something
called negative look-behind assertion, thus:

/(?<!\\)\\'/

This means "a backslash and a quote not immediately preceded by a
backslash". As you can see, the fact that Perl regexps use backslashes
themselves can be confusing...

Two more things. Are you sure your set of keywords is complete? If a
new one is introduced, this'll break unless you add the new keyword.
You can instead just say "\w+=".

But the other thing is, if "Str='......'" can itself contain
"HostGroup=", you're out of luck, and it's time to go to that "real
parser" path.

On Wed, Feb 4, 2009 at 6:06 PM, Chanan Berler <chananb at centerity.com> wrote:
> Currently the only way I found is:
>
> my $str_tmp = $str;
> $str_tmp =~ s/HostGroup='(.*?)'//;
> $str_tmp =~ s/Host='(.*?)'//;
> $str_tmp =~ s/Service='(.*?)'//;
> $str_tmp =~ s/ServiceGroup='(.*?)'//;
> $str_tmp =~ s/Attempt='(.*?)'//;
> $str_tmp =~ s/State='(.*?)'//;
> $str_tmp =~ s/REGEX.*$//;
>
> print $str_tmp;
>
> What do think?
> Thanks again
> Chanan
>
>
> -----Original Message-----
> From: perl-bounces at perl.org.il [mailto:perl-bounces at perl.org.il] On Behalf
> Of Chanan Berler
> Sent: Wednesday, February 04, 2009 5:56 PM
> To: 'Perl in Israel'
> Subject: [Israel.pm] (no subject)
>
>
> Hi All,
>
> I am trying for an hour not to parse this string, thought maybe someone has
> a better idea:
>
> I need to extract the 'and ' 'or' and 'not' from the statement, still keep
> the order (since I need to rebuild the string into sql statement)
> Problem: the Service/ServiceGroup or Host/HosName can have the word 'and' /
> 'or' / 'not'  inside
>
> Str = "HostGroup='generic-hostgroup' and Attempt='5' and
> ServiceGroup='Sample Service View' or Service='WINDOWS CPU' and
> State='Critical' and REGEX=(| and Mem: (.*?)%|,=,80)
>
> Does anyone has any idea ? maybe just the start..
> Thanks
> Chanan
>
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl
>
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl
>



-- 
Gaal Yahas <gaal at forum2.org>
http://gaal.livejournal.com/



More information about the Perl mailing list