Solved

What string matches either of these regular expressions?

Posted on 2014-02-17
6
375 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]
ID: 39864415
in the second one, I would say :
\\4
is wrong, I would put this instead:
([-/]|\\. ?)
0
 

Author Comment

by:purplesoup
ID: 39864420
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
ID: 39864422
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 450 total points
ID: 39864447
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
ID: 39864465
'11-1-11'
should match both expressions
0
 

Author Closing Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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…

770 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