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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:41:35+00:00 2026-05-15T23:41:35+00:00

Question : What’s the simplest way how to test if given Regex matches whole

  • 0

Question: What’s the simplest way how to test if given Regex matches whole string ?

An example:
E.g. given Regex re = new Regex("."); I want to test if given input string has only one character using this Regex re. How do I do that ?

In other words: I’m looking for method of class Regex that works similar to method matches() in class Matcher in Java (“Attempts to match the entire region against the pattern.”).

Edit: This question is not about getting length of some string. The question is how to match whole strings with regular exprestions. The example used here is only for demonstration purposes (normally everybody would check the Length property to recognise one character strings).

  • 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-15T23:41:36+00:00Added an answer on May 15, 2026 at 11:41 pm

    If you are allowed to change the regular expression you should surround it by ^( ... )$. You can do this at runtime as follows:

    string newRe = new Regex("^(" + re.ToString() + ")$");
    

    The parentheses here are necessary to prevent creating a regular expression like ^a|ab$ which will not do what you want. This regular expression matches any string starting with a or any string ending in ab.


    If you don’t want to change the regular expression you can check Match.Value.Length == input.Length. This is the method that is used in ASP.NET regular expression validators. See my answer here for a fuller explanation.

    Note this method can cause some curious issues that you should be aware of. The regular expression “a|ab” will match the string ‘ab’ but the value of the match will only be “a”. So even though this regular expression could have matched the whole string, it did not. There is a warning about this in the documentation.

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

Sidebar

Related Questions

Question: I want to test an if statement in PostgreSQL: IF (SELECT COUNT(*) FROM
Question: I want to untar a tarfile which has many tar files within itself
Question Using XSLT 1.0, given a string with arbitrary characters how can I get
Question Implement a function bool chainable(vector<string> v) , which takes a set of strings
Question: Given a Font object, how do I draw all the glyphs/symbols/characters of the
Question: How can I give a new user almost all privileges, but still keep
Question Does anyone know of a way to round a float to the nearest
Question: input an int number ex: ABCD, verify that is ABCD = AB*CD or
Question is : how to add any event in calender? i want to make
Question : Is there a simple way to print out an array in Ruby

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.