Regular Expression Help

Regular Expression Help

am 16.04.2008 05:20:32 von chung.ley

------_=_NextPart_001_01C89F70.D96843D3
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

Hi,

=20

I have a program that will take in a string that will resolve to a path
where the output is going to store.

=20

The path can includes "variables" in this format "%%".
The acceptable variableNames that the program will support are fixed to
a list such as "Person", "Class", "Dept". This list may grow in the
future; but for now it is fixed.

=20

So, some of the acceptable strings are:

C:\Windows\%Person%\%Class%

C:\MyHomeDir\%Dept%\%Person%

C:\MyHomeDir\%Dept%_%Person%

and etc...

=20

I like to develop a validate method to return false when the string in
between the % pair don't belong to the acceptable list, and was trying
to do that with an expression.

- Look for the string that is in between the "%" by the closest pair.

- The "%" pair must start at the "odd" occurrence and finish on the
"next even" occurrence... Don't know how to describe this exactly, but
I don't want it to pack this up ("InBetween") from this
(C:\MyHomeDir\%Dept%InBetween%Person) even though it is in between a %
pair.

- Then compare the string within the "%" pair to see if it belongs to
the "acceptable" list or not.

=20

Is using a regular expression the right approach here? or I should just
iterate thru?

=20

Thanks...

=20

--Chung Ley


------_=_NextPart_001_01C89F70.D96843D3--

Re: Regular Expression Help

am 16.04.2008 16:24:52 von Rob Dixon

Ley, Chung wrote:
> Hi,
>
>
>
> I have a program that will take in a string that will resolve to a path
> where the output is going to store.
>
>
>
> The path can includes "variables" in this format "%%".
> The acceptable variableNames that the program will support are fixed to
> a list such as "Person", "Class", "Dept". This list may grow in the
> future; but for now it is fixed.
>
>
>
> So, some of the acceptable strings are:
>
> C:\Windows\%Person%\%Class%
>
> C:\MyHomeDir\%Dept%\%Person%
>
> C:\MyHomeDir\%Dept%_%Person%
>
> and etc...
>
>
>
> I like to develop a validate method to return false when the string in
> between the % pair don't belong to the acceptable list, and was trying
> to do that with an expression.
>
> - Look for the string that is in between the "%" by the closest pair.
>
> - The "%" pair must start at the "odd" occurrence and finish on the
> "next even" occurrence... Don't know how to describe this exactly, but
> I don't want it to pack this up ("InBetween") from this
> (C:\MyHomeDir\%Dept%InBetween%Person) even though it is in between a %
> pair.
>
> - Then compare the string within the "%" pair to see if it belongs to
> the "acceptable" list or not.
>
>
>
> Is using a regular expression the right approach here? or I should just
> iterate thru?

If the names are environment variable names, which is implied by your
syntax, then you can substitute the values of those variables as shown
in the program below.

HTH,

Rob


use strict;
use warnings;

my $str = '%windir%\system32';

$str =~ s/%(.*?)%/$ENV{uc $1}/g;

print $str;

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/