Solved

What string matches either of these regular expressions?

Posted on 2014-02-17
6
373 Views
Last Modified: 2014-02-17
An in-house web app is asking for a date but errors whatever I put in.

I turned on the debugger and I can see it is trying a couple of regex matches:

^\\s*(\\d{1,2})([-/]|\\. ?)(\\d{1,2})(?:\\s|\\2)((\\d{4})|(\\d{2}))(?:\\s\u0433\\.)?\\s*$

and

^\\s*((\\d{4})|(\\d{2}))([-/]|\\. ?)(\\d{1,2})\\4(\\d{1,2})\\.?\\s*$

I'll put the code below.

I've tried some obvious dates - 2014-02-17 or 02/17/2014 or 02/17/14 but in the arrays generated there always seems to be an element that is null that causes an error in the code.

Can anyone figure out a string that would match one of the expressions?

Here is the code:

    else if (dataType == "Date") {
        var yearFirstExp = new RegExp("^\\s*((\\d{4})|(\\d{2}))([-/]|\\. ?)(\\d{1,2})\\4(\\d{1,2})\\.?\\s*$");
        m = op.match(yearFirstExp);
        var day, month, year;
        if (m != null && (m[2].length == 4 || val.dateorder == "ymd")) {
            day = m[6];
            month = m[5];
            year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10))
        }
        else {
            if (val.dateorder == "ymd"){
                return null;
            }
            var yearLastExp = new RegExp("^\\s*(\\d{1,2})([-/]|\\. ?)(\\d{1,2})(?:\\s|\\2)((\\d{4})|(\\d{2}))(?:\\s\u0433\\.)?\\s*$");
            m = op.match(yearLastExp);
            if (m == null) {
                return null;
            }
            if (val.dateorder == "mdy") {
                day = m[3];
                month = m[1];
            }
            else {
                day = m[1];
                month = m[3];
            }
            year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10))
        }
        month -= 1;
        var date = new Date(year, month, day);
        if (year < 100) {
            date.setFullYear(year);
        }
        return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? date.valueOf() : null;

Open in new window

0
Comment
Question by:purplesoup
  • 3
  • 2
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
in the second one, I would say :
\\4
is wrong, I would put this instead:
([-/]|\\. ?)
0
 

Author Comment

by:purplesoup
Comment Utility
Thanks - I can't actually change the code, I was just hoping someone might suggest a value that would be accepted by the regex without causing a validation error?
0
 

Author Comment

by:purplesoup
Comment Utility
Probably it would have been clearer if I had said, instead of

"Can anyone figure out a string that would match one of the expressions?"

this

"Can anyone figure out a string that would match one of the expressions so I could enter the date in the application?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 450 total points
Comment Utility
I checked with this page:
http://regex101.com/#javascript
where I put this expression (removing the double \\ which is for C# string ...)
^\s*((\d{4})|(\d{2}))([-/]|\. ?)(\d{1,2})\4(\d{1,2})\.?\s*$

the page explains the expression, and typing this:
2014-1-41

it shows that this one matches. the \\4 is to match back the - from the before ...
that is still not a date, but at least a format ...
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 50 total points
Comment Utility
'11-1-11'
should match both expressions
0
 

Author Closing Comment

by:purplesoup
Comment Utility
Thanks - I was using a different checker and it wasn't matching - I'll use that one from now on.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
.NET Error 7 42
Hovering effect 9 28
How can I use this extension method? 8 35
Spacing between controls 4 15
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now