Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 263691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:37:23+00:00 2026-05-11T22:37:23+00:00

I have a Perl script, that’s supposed to match this string: Sometimes, he says

  • 0

I have a Perl script, that’s supposed to match this string:

Sometimes, he says "hey fred, what's up?"

It says if it found fred at the beginning, end, or middle of the word, or if it just found “fred”. So it matches Alfred, and Frederich.

Well, in this string, it’s supposed to say it found fred on its own, but it’s saying it found it at the beginning of a word. Here is the regex for the beginning-of-word-fred, (it’s in an if-elsif ladder going beginning of word, end of word, just fred, middle of word):

if(/.*\s+[fF][rR][eE][dD][^ \t\r\n,.:;'"].*/){
    print "found fred at beginning of a word:\n    $_\n";

I used [^ \t\r\n,.:;'"] instead of \S incase the word is followed by some punctuation. Obviously it’s not an exhaustive list of punctuation, but it doesn’t matter for this example since it’s followed by a comma.

this is in a foreach loop… If it means anything, This is exercise 7-1 in Learning Perl 5th ed.

update

the exercise in the book is to write a Perl program to find “fred” in a list of words. Then it asks, does the script find fred in “Frederich” or “Alfred?” And then it says to write a text file that talks about Fred Flinstone and his friends, and use it as an input to the script.

also

I figured it out, sort of:
I must have changed something while writing the question that I forgot about: I tested it again and instead of matching the beginning of a word, it just said it found it anywhere. So the problems wasn’t that it thought it was at the beginning of a word, it was that it thought it wasn’t the only thing in the word. I added [,.:;'"]?\s+ to the code which matches “fred” as a whole word and it worked. I guess I should have thought about it a little more before asking 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T22:37:23+00:00Added an answer on May 11, 2026 at 10:37 pm

    if you want to match Fred and frederick but not Alfred, then your regex is:

    /\bfred\w*\b/i
    

    That is to say: a word boundary followed by (case-insentitive) “fred” followed by zero or more word-characters, followed by another word boundary. If you just want frederick, but plain Fred is out, then:

    /\bfred\w+\b/i
    

    i.e., word boundary, “fred”, one or more word chars, word boundary.

    UPDATE: re-reading your question, it seems that you want:

    perl -E '
    use strict;
    use warnings;
    for( "nobody is here",
        "I am Frederick Flintsone",
        "she is alfredine",
        "I am Alfred Hitchcock",
        "fred has left the building" ) {
      say;
      if( ! /\b(\w*)fred(\w*)\b/i ) {
        say "no fred!"
      } elsif( ! length "$1$2" ) {
        say "fred by itself!"
      } elsif( ! length $2 ) {
        say "something-fred!"
      } elsif( ! length $1 ) {
        say "fred-something!"
      } else {
        say "something-fred-something!"
      }
    }'
    

    that outputs:

    nobody is here
    no fred!
    I am Frederick Flintsone
    fred-something!
    she is alfredine
    something-fred-something!
    I am Alfred Hitchcock
    something-fred!
    fred has left the building
    fred by itself!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 157k
  • Answers 157k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try to give width:100%; to the table inside the div,… May 12, 2026 at 11:08 am
  • Editorial Team
    Editorial Team added an answer First, use Instrument's Object Graph instrument to verify that the… May 12, 2026 at 11:08 am
  • Editorial Team
    Editorial Team added an answer As far as I know the only way is to… May 12, 2026 at 11:08 am

Related Questions

I have a Perl script, that's supposed to match this string: Sometimes, he says
I have a Perl script that requires two command line arguments that takes a
I have a Perl script that I'm attempting to set up using Perl Threads
I have a Perl script that sets up variables near the top for directories
I have a Perl script that I'd like to run on Windows, using either

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.