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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:03:07+00:00 2026-06-04T09:03:07+00:00

I need a regex to match expressions which contain the string OKAY then a

  • 0

I need a regex to match expressions which contain the string OKAY then a possible hyphen, and then zero or one word characters. after this any non-word-character is accepted and then anything. for expressions which match, OKAY will be changed to OK if there is no word-character following, and to e.g: OA if the letter following is A. if the hyphen exists it is dropped.

OKAY         =>       OK
OKAY-        =>       OK
OKAYA        =>       OA
OKAY-A       =>       OA
OKAYAB       =>       OKAYAB          (no-match)
OKAY-AB      =>       OKAY-AB         (no-match)

examples may be followed by e.g: .CD without changing the results

OKAY.CD         =>       OK.CD
OKAY-.CD        =>       OK.CD
OKAYA.CD        =>       OA.CD
OKAY-A.CD       =>       OA.CD
OKAYAB.CD       =>       OKAYAB.CD          (no-match)
OKAY-AB.CD      =>       OKAY-AB.CD         (no-match)

my problem implementing this was that since both the hyphen and the word-character are optional, I get “lazy” matches which match also the non-wanted cases.
for the sake of education I would appreciate examples both with and without look-aheads (if possible).

  • 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-04T09:03:09+00:00Added an answer on June 4, 2026 at 9:03 am

    Here is a regex that should work for you:

    \bOKAY(?>-?)(\w)?([^\w\s]\S*)?(?!\S)
    

    Since it isn’t clear what language you are using, here is pseudo code for how you would do the replacement.

    "O" + (match.group(1) if match.group(1) else "K") + match.group(2)
    

    Here is a rubular: http://www.rubular.com/r/SE8MBkUUUo


    edit: I made some changes in the above regex after the comments, but the description below does not reflect those changes. Here are the changes from the original regex:

    • Changed ^ to \b so it doesn’t need to start at beginning of line
    • \W became [^\w\s], this prevents OKAY OKAY from being one match
    • Changed .* to \S* so the match will end at whitespace
    • Changed $ to (?!\S), (?!\S) means “only match if we are at the end of the string or the next character is whitespace”, could also be written as (?=\s|\z)

    The really tricky part here is that a regex like ^OKAY-?(\w)?(\W.*)?$ looks like it would work, but it does not for a case like OKAY-AB because in the end both the -? and the (\w)? will not match, and then (\W.*)? will match the remainder of the string.

    What we need to do to fix this is make it so -? will not backtrack. This would be simple if possessive quantifiers were supported by .NET, then we could just change it to -?+.

    Unfortunately they aren’t supported, so we need to use atomic grouping instead. (?>-?) will optionally match a -, but will forget all backtracking information as soon as it exits the group. Note that the atomic group does not capture, so (\w)? is capture group 1.

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

Sidebar

Related Questions

I've a String template which I need to process using regex. I need to
I need a regex that will match blahfooblah but not blahfoobarblah I want it
I need some help. I am looking for a regex that would match the
I'm facing a problem with Regex... I had to match sharepoint URL.. I need
i need a regexp to match only single / in a string in PHP.
Possible Duplicate: What is the ultimate postal code and zip regex? I need Regex
I need a Regex I think to see if a string is only a
Possible Duplicate: Regular expression to match hostname or IP Address? I need to validate
I need a regular expression to help me make a match in my string.
I have a text field which I need to validate using a regex. My

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.