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

  • SEARCH
  • Home
  • 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 8363411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:10:08+00:00 2026-06-09T12:10:08+00:00

I wrote a regular expression to validate strings that must adhere to the following

  • 0

I wrote a regular expression to validate strings that must adhere to the following rules:

  1. Must be at least one character
  2. Must contain no blank characters
  3. First character may not be punctuation
  4. Last letter may not be punctuation
  5. May not end in punctuation followed by digits
  6. All other characters may be any UTF-8 character other than /[:@#].

Here is the regex:

my $name_re = qr/
     [^[:punct:][:blank:]]      #  not punct or blank
     (?:                        #  followed by...
         [^[:blank:]:@#]*       #      any number non-blank, non-@, non-#, non-@
         [^[:punct:][:blank:]]  #      one not blank or punct
     )?                         #  ... optionally
/x;

See anything missing? Rule #5 is not enforced. I’ve been enforcing it by writing code such as this:

die "$proj is not a valid name" unless $proj =~ /\A$name_re\z/
    && $proj !~ /[[:punct:]][[:digit:]]+\z/;

There are a bunch of places I have to do this, so I would rather that it all be done in a single regular expression. The question is: how? What regular expression would reject a value such as “foo,23”?

  • 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-06-09T12:10:09+00:00Added an answer on June 9, 2026 at 12:10 pm

    The following should work:

    my $name_re = qr/
        \A(?![[:punct:]])         # first character isn't punctuation
        (?:                       # start non-capturing group, repeated once or more
           (?![[:punct:]][[:digit:]]+\z)  # make sure 5th condition isn't violated
           [^[:blank:]:@#]                # match a valid character
        )+                        # end non-capturing group
        (?<![[:punct:]])\z        # last character isn't punctuation
    /x;
    

    Note that I moved the anchors inside of the regex, this may not be completely necessary with your current method but I think it makes it more clear to have it all in one place.

    (?!...) and (?<!...) are negative lookaheads and lookbehinds, respectively. They make it pretty simple to verify things like this, essentially the middle section can be “match these valid characters”, with the lookahead/lookbehind at beginning and end to check those conditions.

    The negative lookahead in the middle verifies that at the given position, we cannot match to the end of the string with only punctuation or digits, or in other words it checks to make sure that condition 5 isn’t violated. Because this lookahead is within the repeated group, it is checked at every position.

    This would be simpler if you could use a variable length lookbehind, but I don’t think Perl supports them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I write regular expression in C# to validate that the input does
I'm trying to write a regular expression that will validate that user input is
I wrote a regular expression that parses a file path into different group (DRIVE,
I wrote a regular expression that will replace odd number of slashes by an
I wrote a regular expression in hope that I will be able to replace
I wrote a Regular Expression that generates a url like /abc/deutschland/bbs-tagesfahrten/betz-mode-frotier-center-–-tress-teigwaren.html. Now I want
Hello i wrote a regular expression to validate URL jQuery.validator.addMethod(url_validation, function (value, element) {
I'm trying to write a regular expression that will validate a variable assignments in
I am newbie to java regular expression. I wrote following code for validating the
Possible Duplicate: Regex to match URL I wrote a regular expression to validate only

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.