Regex problem extracting middle-word part of string

Regex problem extracting middle-word part of string

Post by Rohesia Hamilton Metcal » Sat, 01 Jun 2002 00:28:45



Hello,

I have a list of html-page-creating cgi scripts, which is being
randomly accessed. Once my random selection of the script has been
made, I need to be able to access the middle part of the script name to
use this part to complete the path to an html page that needs to be
called by the selected cgi-script (which is creating a frameset).

I've tried to do this with a regex, but my regex isn't doing it. Here's
my code:

##############################

#!/usr/local/bin/perl5
use CGI qw(:standard);                  


"f-eeeeee.cgi", "f-ffffff.cgi");

# pick one at random
srand;

#here's the non-working regex:
$ScriptName =~ s/$RandomScript/\w{3,}\b/;

$RandomScriptURL = "path/".$ScriptName.".htm";

##################################

Everything here is working except my attempt at a regex. I'm trying to
get rid of "f-" at the begining and the ".cgi" at the end. I thought
\w{3,}\b would get me the first block of word characters that came in a
set of three or more until the end of the word. I'm getting nothing at
all from this and am not sure what I'm missing.

Any help much appreciated.

Rohesia

=====
Rohesia Hamilton Metcalfe

__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com

 
 
 

Regex problem extracting middle-word part of string

Post by Jeff 'Japhy' Piny » Sat, 01 Jun 2002 00:37:17


On May 30, Rohesia Hamilton Metcalfe said:

>#!/usr/local/bin/perl5
>use CGI qw(:standard);                      

>#make array of cgi-scripts:

>"f-eeeeee.cgi", "f-ffffff.cgi");

># pick one at random
>srand;

>#here's the non-working regex:
>$ScriptName =~ s/$RandomScript/\w{3,}\b/;

Where did you get $ScriptName from?  What is it supposed to hold?  Also,
you can't use a regex as the right-hand side of a s///.  s/pattern/text/
is the way s/// is used.

Perhaps you want to do:

  ($ScriptName) = $RandomScript =~ /(\w{3,}\b)/

That will match the regex /(\w{3,}\b)/ against the string in $RandomScript
and assign the matched portion to $ScriptName.

Quote:>$RandomScriptURL = "path/".$ScriptName.".htm";

This looks cleaner if you put the variable in the quoted string, instead
of using the . operator:

  $RandomScriptURL = "path/$ScriptName.htm";

--

RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]

 
 
 

Regex problem extracting middle-word part of string

Post by Drie » Sat, 01 Jun 2002 03:08:03



> # pick one at random
> srand;

> #here's the non-working regex:
> $ScriptName =~ s/$RandomScript/\w{3,}\b/;

> $RandomScriptURL = "path/".$ScriptName.".htm";

> ##################################

> Everything here is working except my attempt at a regex. I'm trying to
> get rid of "f-" at the begining and the ".cgi" at the end. I thought
> \w{3,}\b would get me the first block of word characters that came in a
> set of three or more until the end of the word. I'm getting nothing at
> all from this and am not sure what I'm missing.

[..]


on the main thrust since a simple piece of perl like


        "f-eeeeee.cgi", "f-ffffff.cgi");

        # pick one at random
        srand;
        my $RandomScript = $Scripts[int(rand($#Scripts + 1))];

        print "J Randome Script name :$RandomScript:\n";
        #
        # now to make it sans f- with
        my ($ScriptName) = $RandomScript =~ /(\w{3,}\b)/;
        print "J Randome Script name :$RandomScript: undt :$ScriptName:\n";

will show you that you get into $ScriptName - what you want.

I am also a bit concerned with trying to seed rand() with a
list, rather than say, the count of the list as noted above.

never be afraid to step aside, whip out a silly bit in YourTmpDirHere
and bash the semantics till it does what you really want...
since you do recall that it provides for a random value
from 0 to less than expr - hence you want (index + 1) so
that you can get your last element in the list....

ciao
drieux

---

 
 
 

Regex problem extracting middle-word part of string

Post by Jeff 'Japhy' Piny » Sat, 01 Jun 2002 03:13:46


On May 30, drieux said:

>> srand;

>    my $RandomScript = $Scripts[int(rand($#Scripts + 1))];

>I am also a bit concerned with trying to seed rand() with a
>list, rather than say, the count of the list as noted above.

>never be afraid to step aside, whip out a silly bit in YourTmpDirHere
>and bash the semantics till it does what you really want...
>since you do recall that it provides for a random value
>from 0 to less than expr - hence you want (index + 1) so
>that you can get your last element in the list....



I'm more concerned about the use of srand(), which is superfluous in
recent Perls.  The int() is also superfluous.


--

RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]

 
 
 

Regex problem extracting middle-word part of string

Post by Drie » Sat, 01 Jun 2002 03:14:35


[..]

Quote:

> Any help much appreciated.

> Rohesia

perlsonally I'm a fore and aft fan and would have done it like

        my $fore = 'f-';
        my $aft = '.cgi';

        my $ScriptName = $1 if ($RandomScript =~ /$fore # all the stuff before
it
                                                                  (.*)          # $1 - the stuff we want
                                                                  $aft/xo);     # all the stuff aft of it

        print "foreand aft says :$RandomScript: undt :$ScriptName:\n";

which will be more 're-usable' in the long run, and
of course has the if check as welll...

ciao
drieux

---

 
 
 

Regex problem extracting middle-word part of string

Post by Drie » Sat, 01 Jun 2002 03:27:06



> On May 30, drieux said:

>>> srand;

>>        my $RandomScript = $Scripts[int(rand($#Scripts + 1))];
[..]


> I'm more concerned about the use of srand(), which is superfluous in
> recent Perls.  The int() is also superfluous.

good to hear that we do not have to go back quite to the perl4 tricks.



that works - but it makes me nervous...

http://www.wetware.com/drieux/pbl/perlTrick/randPick.txt

ciao
drieux

---

 
 
 

Regex problem extracting middle-word part of string

Post by Jeff 'Japhy' Piny » Sat, 01 Jun 2002 03:44:52


On May 30, drieux said:


>that works - but it makes me nervous...

You've no need to feel uneasy.  It doesn't work because of cruftiness --
it's merely combining known facts about how Perl operates:

  1. rand() expects a scalar
  2. array indices are integers

In fact, the Perl FAQ does it this way (albeit in two steps):


  $element = $array[$index];

--

RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]

 
 
 

Regex problem extracting middle-word part of string

Post by Drie » Sat, 01 Jun 2002 05:41:25



> On May 30, drieux said:


>> that works - but it makes me nervous...

> You've no need to feel uneasy.  It doesn't work because of cruftiness --

I was perchance not clear - I had been doing stuff that
way for, well a long time.... not that this is crufty....

it still makes me nervous... these newFangledThings and all....

> it's merely combining known facts about how Perl operates:

>   1. rand() expects a scalar
>   2. array indices are integers

> In fact, the Perl FAQ does it this way (albeit in two steps):


>   $element = $array[$index];

that I expect - and would have used up until your kvetch,
the first step foward was to skip the $index - you know
'chance it'.... this whole 'lisp' style of coding....

Oh I see, now I get where we slipped the disk....

srand was required up to 5.004....

and there is your FAQ

        "How do I select a random element from an array?"

with the


as I originally coded it.....

ciao
drieux

---

"That's the problem with kids these days,
        no respect for their elders....
        why when we were growing up we had 'the doors'...."

 
 
 

Regex problem extracting middle-word part of string

Post by Janek Schleich » Wed, 05 Jun 2002 23:06:46


Rohesia Hamilton Metcalfe wrote at Thu, 30 May 2002 17:28:45 +0200:

> #make array of cgi-scripts:

> "f-ffffff.cgi");

> # pick one at random
> srand;


I agree to Drieux that such a random selection looks strange.
I always feel nervous when i see Jeff's solution.
Not because of the underlying context what let's a floating value becomes an int,

And there's double code (things), what is a real crime :-))

Let's look at an existing module:

use Tie::Pick;

There's a disadventage, too.
Tie::Pick removes the element chosen,
but when it doesn't play a role or is even wanted,
I'd suggest the above solution.
Otherwise I'd prefer Jeff's solution.

Greetings,
Janek

 
 
 

Regex problem extracting middle-word part of string

Post by Rohesia Hamilton Metcal » Thu, 06 Jun 2002 23:55:07


Thanks Janek and Japhy and Drieux for all the help on this!

I've yet to look at this Tie::Pick, but will.

In the meantime, I've gone with the Japhy solution,


....because it was the simplest (and works).

I didn't quite see why you would have to do a +1 on the following:

   my $RandomScript = $Scripts[int(rand($#Scripts + 1))];


anyway? Such that you'd want the numbers 0-3 for a 4-element array
rather than 1-4? (unless rand only works with numbers greater than zero
and, in Japhy's solution, is doing this implicitly?)

Still, grateful to know I can skip the srand; line. When I started
this, the first random seeding code I came across was even more typing:

   srand(time() ^ ($$ + ($$ << 15)) );

Well, that worked, too, but...

Anyway, again, many thanks for all,

Rohesia


> Rohesia Hamilton Metcalfe wrote at Thu, 30 May 2002 17:28:45 +0200:

> > #make array of cgi-scripts:

> "f-eeeeee.cgi",
> > "f-ffffff.cgi");

> > # pick one at random
> > srand;

> I agree to Drieux that such a random selection looks strange.
> I always feel nervous when i see Jeff's solution.
> Not because of the underlying context what let's a floating value
> becomes an int,

> And there's double code (things), what is a real crime :-))

> Let's look at an existing module:

> use Tie::Pick;

> There's a disadventage, too.
> Tie::Pick removes the element chosen,
> but when it doesn't play a role or is even wanted,
> I'd suggest the above solution.
> Otherwise I'd prefer Jeff's solution.

> Greetings,
> Janek

> --



=====
Rohesia Hamilton Metcalfe

__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com

 
 
 

Regex problem extracting middle-word part of string

Post by Felix Geerinc » Fri, 07 Jun 2002 00:17:51


Quote:> I didn't quite see why you would have to do a +1 on the following:

>    my $RandomScript = $Scripts[int(rand($#Scripts + 1))];


and $number (exclusive). the 'int' function throws away the fractional
part.

If you don't add 1, you would never get the last array-element.

--
felix

 
 
 

1. Thanks - Re: Regex problem extracting middle-word part of str ing

shouldn't it be written as this to aviod that confusion:

----------------------------------------------------------------------------
--------------------
The views and opinions expressed in this email message are the sender's
own, and do not necessarily represent the views and opinions of Summit
Systems Inc.

2. Computer Networking SIG

3. need help extracting words from a string - Thanks and need more help

4. Getting file handle from FILE

5. need help extracting words from a string

6. Q: distiller launched from dvips (FAQ?)

7. Extract part of variable lenght string

8. What's a Pass? (70-316)

9. Extract Middle Name from Text

10. strings, strings, strings ...

11. Extract text from position x as string

12. Extracting a String from an Any type

13. extracting a string from between parens