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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:24:10+00:00 2026-05-13T01:24:10+00:00

yes it’s another .net regex question :) (please excuse the long waffle leading up

  • 0

yes it’s another .net regex question 🙂 (please excuse the long waffle leading up to the actual question)

I’m allowing users to use simple date/time macros for entering dates quickly (they don’t want a date picker)

for example they can enter:
d +1d -2h
this will give them a date time string of todays’s date, plus one day, minus two hours.

anyways I’ve created a regex to match these which works fine (probably not the best way to do it but it works!):
\b[DTdt]( *[+-] *[1-9][0-9]* *[dDhHmMwW])*\b

as you’ve probably guessed i’m using the regex to validate these entries before parsing them to calculate the resulting datetime. At first I used something like:

Regex rgxDateTimeMacro = new Regex(@"\b[DTdt]( *[+-] *[1-9][0-9]* *[dDhHmMwW])*\b");

if(rgxDateTimeMacro.isMatch(strInput)){
  ...string passes...
}

I then quickly realised that isMatch returns true if there’s any matches in the passed string,
d +1d +1
would return true ^__^

so i changed it around to do something like this:

Regex rgxDateTimeMacro = new Regex(@"\b[DTdt]( *[+-] *[1-9][0-9]* *[dDhHmMwW])*\b");
MatchCollection objMatches = rgxDateTimeMacro.Matches(strInput);

if (objMatches.Count > 0)
{
    // to pass.. we need a match which is the same length as the input string...
    foreach (Match m in objMatches)
    {
        if (m.Length == strInput.Length)
        {
            ...string passes...
        }
    }
}

now this works fine, but my question is this: is there a simpler way to check if a string (the whole string) matches a regex? I’ve had a google around, but cant seem to find an obvious answer.

hope this makes sense

Pete


UPDATE

thanks for all the quick answers, ^$ does the trick : )

(showing my inexperience with regexes ^__^)

  • 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-13T01:24:11+00:00Added an answer on May 13, 2026 at 1:24 am

    If I understand correctly, use: ^my regex$

    ^ – Start of the string
    $ – End of the string

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

Sidebar

Related Questions

Yes, I know, yet another question about mutable objects. See this for general background
Yes, another IE-only problem for your Thursday. :) http://jsfiddle.net/dex3703/nwTUm/ This layout looks fine in
Yes another question that is a little Unique because I cant find the same
Yes, another NULL vs empty string question. I agree with the idea that NULL
Yes, I'm yet another person to as this annoying question... but I'm not convinced
Yes, I know that in ASP.NET MVC you have to use ViewModels. But I'm
Yes, I want to use GIMP to create a transparent image with some text
Yes, I know this question has been asked before, but I can't find an
Yes, I know this question has been asked a lot of times, but I
Yes, the only question is whether I'm doing it correct or not. And maybe

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.