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 8589923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:10:27+00:00 2026-06-11T23:10:27+00:00

I want to use a regular expression to check a string to make sure

  • 0

I want to use a regular expression to check a string to make sure 4 and 5 are in order. I thought I could do this by doing

'$string =~ m/.45./'

I think I am going wrong somewhere. I am very new to Perl. I would honestly like to put it in an array and search through it and find out that way, but I’m assuming there is a much easier way to do it with regex.

print "input please:\n";
$input = <STDIN>;
chop($input);
if ($input =~ m/45/ and $input =~ m/5./) {
    print "works";
}
else {
    print "nata";
}

EDIT: Added Info
I just want 4 and 5 in order, but if 5 comes before at all say 322195458900023 is the number then where 545 is a problem 5 always have to come right after 4.

  • 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-11T23:10:28+00:00Added an answer on June 11, 2026 at 11:10 pm

    Assuming you want to match any string that contains two digits where the first digit is smaller than the second:

    There is an obscure feature called "postponed regular expressions". We can include code inside a regular expression with

    (??{CODE})
    

    and the value of that code is interpolated into the regex.

    The special verb (*FAIL) makes sure that the match fails (in fact only the current branch). We can combine this into following one-liner:

    perl -ne'print /(\d)(\d)(??{$1<$2 ? "" : "(*FAIL)"})/ ? "yes\n" :"no\n"'
    

    It prints yes when the current line contains two digits where the first digit is smaller than the second digit, and no when this is not the case.

    The regex explained:

    m{
       (\d)   # match a number, save it in $1
       (\d)   # match another number, save it in $2
       (??{   # start postponed regex
          $1 < $2      # if $1 is smaller than $2
          ? ""         # then return the empty string (i.e. succeed)
          : "(*FAIL)"  # else return the *FAIL verb
       })     # close postponed regex
    }x;       # /x modifier so I could use spaces and comments
    

    However, this is a bit advanced and masochistic; using an array is (1) far easier to understand, and (2) probably better anyway. But it is still possible using only regexes.


    Edit

    Here is a way to make sure that no 5 is followed by a 4:

    /^(?:[^5]+|5(?=[^4]|$))*$/
    

    This reads as: The string is composed from any number (zero or more) characters that are not a five, or a five that is followed by either a character that is not a four or the five is the end of the string.

    This regex is also a possibility:

    /^(?:[^45]+|45)*$/
    

    it allows any characters in the string that are not 4 or 5, or the sequence 45. I.e., there are no single 4s or 5s allowed.

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

Sidebar

Related Questions

hi i want to use regular expression with this kind of string, but i
I want to use regular expression same way as string.Format. I will explain I
I want to use this regular expression in Python: <(?:[^]*[']*|'[^']*'[']*|[^'>])+> (from RegEx match open
I want a regular expression to use in preg_match to check whether or not
I want to use regular expression to find strings in a file that have
I have that text : <a href=/extend/themes/bizway>BizWay</a> And i want to use regular expression
I'm trying to use regular expression right now and I'm really confuse. I want
I'm using Python and I want to use regular expressions to check if something
How to use php regular expression to get the position of date string in
In C# , I want to use a regular expression to match any of

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.