×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How do I search a line of text for known string

How do I search a line of text for known string

How do I search a line of text for known string

(OP)
Hi

I have a variable ($line) that contains multiple instances of the string "web/" followed by an unknown 6 character string.  Is there any way I can search for every instance of web/ then readthe next 6 characters into another variable (for inclusion in a file/display?)

Thanks
Will

RE: How do I search a line of text for known string

Try this -

$' returns everything after a matched pattern.
$1 returns something remembered with brackets in a pattern


    $_ = $line;

    while(/web\/(......)/){
        $charstr=$1; # this is the string you were looking for
        $_=$'; # chop everything off $_ before the current match
        # do something with $charstr
    }


Mike

Mike Lacey
Mike_Lacey@Cargill.Com
Cargill's Corporate Web Site

RE: How do I search a line of text for known string

(OP)
Hi Mike

That was really great.  It was exactly what I was looking for.  any suggestions on learning resources?

Thanks
Will

fortytwo
will@hellacool.co.uk
my test site

RE: How do I search a line of text for known string

Will,

Glad it worked.

The place to start for perl resources is http://www.perl.com and my main reference book is "Programming Perl" from O'Reilly (ISBN 1-56592-149-6), I wouldn't be without this book - and it has a nice picture of a camel on the front as well.

Whilst I think about it - that first line in my example:

$_ = $line

Was just to make my example code clearer as most (a lot any) perl things work on $_ by default.

Without that line the pattern matching would be written

while($line =~ /web\/(......)/){

Regards,

Mike.

Mike Lacey
Mike_Lacey@Cargill.Com
Cargill's Corporate Web Site

RE: How do I search a line of text for known string

$_ is the variable that stores the current working variable, if that maeks sense... in a for loop, you could say:


foreach (@array) {
  print $_;
}

... and it would print every element in the array

RE: How do I search a line of text for known string

$_ is a sort of catch-all default.

Lot's of things work on $_ unless you say otherwise. print; for example is the same as saying print $_;, chomp; the same as chomp $_;
and $_ ~= /somestring/; does the same as /somestring/;

I tend to use the defaults without specifying them - but I probably shouldn't. When others are trying to read your code it can be a problem. Perl code that uses defaults looks incredibly cryptic to a beginner.

Mike

Mike Lacey
Mike_Lacey@Cargill.Com
Cargill's Corporate Web Site

RE: How do I search a line of text for known string

(OP)
So basically, if a variable is not specified the data goes into $_  as in:

foreach (@array) {
  print $_;
}

which does the same as:

foreach $section (@array) {
  print $section;
}

so you do not have to specify a variable, the variable is the sort of default variable.  Or have i missed the mark (likely)

Will

fortytwo
will@hellacool.co.uk
my test site

RE: How do I search a line of text for known string

That's right, same thing happens here:


open(F, "an_input_file") ¦¦ die;
while(<F>){
    print if /text i am searching for/o;
}


This bit of code prints all lines in the file an_input_file that contain the text "text i am searching for".

It uses the default $_ thingy twice on the same line, once as the default argument to print and again as the default variable to search (as in /text i am searching for/)

Mike
Mike_Lacey@Cargill.Com
Cargill's Corporate Web Site

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close