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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:42:54+00:00 2026-06-16T18:42:54+00:00

Using regular expressions I want to match a word which starts with a letter

  • 0

Using regular expressions I want to match a word which

  • starts with a letter
  • has english alpahbets
  • numbers, period(.), hyphen(-), underscore(_)
  • should not have two or more consecutive periods or hyphens or underscores
  • can have multiple periods or hyphens or underscore

For example,

flin..stones or flin__stones or flin–stones

are not allowed.

fl_i_stones or fli_st.ones or flin.stones or flinstones

is allowed .

So far My regular expression is ^[a-zA-Z][a-zA-Z\d._-]+$

So My question is how to do it using regular expression

  • 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-16T18:42:55+00:00Added an answer on June 16, 2026 at 6:42 pm

    You can use a lookahead and a backreference to solve this. But note that right now you are requiring at least 2 characters. The starting letter and another one (due to the +). You probably want to make that + and * so that the second character class can be repeated 0 or more times:

    ^(?!.*(.)\1)[a-zA-Z][a-zA-Z\d._-]*$
    

    How does the lookahead work? Firstly, it’s a negative lookahead. If the pattern inside finds a match, the lookahead causes the entire pattern to fail and vice-versa. So we can have a pattern inside that matches if we do have two consecutive characters. First, we look for an arbitrary position in the string (.*), then we match single (arbitrary) character (.) and capture it with the parentheses. Hence, that one character goes into capturing group 1. And then we require this capturing group to be followed by itself (referencing it with \1). So the inner pattern will try at every single position in the string (due to backtracking) whether there is a character that is followed by itself. If these two consecutive characters are found, the pattern will fail. If they cannot be found, the engine jumps back to where the lookahead started (the beginning of the string) and continue with matching the actual pattern.

    Alternatively you can split this up into two separate checks. One for valid characters and the starting letter:

    ^[a-zA-Z][a-zA-Z\d._-]*$
    

    And one for the consecutive characters (where you can invert the match result):

    (.)\1
    

    This would greatly increase the readability of your code (because it’s less obscure than that lookahead) and it would also allow you to detect the actual problem in pattern and return an appropriate and helpful error message.

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

Sidebar

Related Questions

I want to match D11-RONPLAYER_DEF_15_PO using this regular expression: D\[0-9]+-\[A-Z]*PLAYER_(DEF\[0-9]*)_(\[^_]+)_ but it does not
I want to validate mathematical expressions using regular expression. The mathematical expression can be
If I want to analyze a string using dozens of regular-expressions, could either the
I am trying to match words using a regular expression, but sadly the word
Hey all, i want to use a regular expression to match a word with
I want to extract text from a column using regular expressions in Oracle 11g.
I am looking for a way to do a fuzzy match using regular expressions.
I'm using Oracle regural expressions and I want to match some string, then something
I want to validate input in a C# TextBox by using regular expressions. The
I'm using Java regular expressions to match and capture a string such as: 0::10000

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.