0

I am new to regex, and have looked all over, though I could not find an exact regex that works. There are no whitespaces in the string, and the dates can be surrounded by any random text, non-date, characters.

Sample Strings and expected responses:

EX 1:

From2017-01-01to2017-12-31_custom_EquityPurchaseAgreementMember_custom_FirstPaymentMember_currency_CNY  
var res = ['2017-01-01', '2017-12-31']

EX 2:

From2016-01-01to2016-12-31  
var res = ['2016-01-01', '2016-12-31']

EX 3:

From2017-01-01to2017-12-31_custom_EquityPurchaseAgreementMember_custom_FirstPaymentMember  
var res = ['2017-01-01', '2017-12-31']

EX 4:

AsOf2017-12-31  
var res = ['2017-12-31']

I have teted this regex (among others), with no avail: /\b\d{4}-\d{2}-\d{2}\b/g

I was using this tool to test: http://rubular.com/r/bce4IHyCjW

1
  • Just reminding that regex has some limitations, because it'll match things like 2017-02-29 (February 29th, but 2017 isn't a leap year) and 2017-04-31 (April 31st, but April has 30 days), not to mention cases like 9999-99-99. Actually, with regex, you'll get a list of possible dates (things that look like dates), but you'll have to validate them later if you want to make sure they're really valid dates. Of course you can ignore this if you can guarantee that the inputs always have valid dates. Commented Apr 4, 2018 at 18:41

4 Answers 4

2

You do not match the dates because the word boundary \b does not match for example between m2

Try it without the word boundary \b

\d{4}\-\d{2}\-\d{2}

const strings = [
  "From2017-01-01to2017-12-31_custom_EquityPurchaseAgreementMember_custom_FirstPaymentMember_currency_CNY",
  "From2016-01-01to2016-12-31",
  "From2017-01-01to2017-12-31_custom_EquityPurchaseAgreementMember_custom_FirstPaymentMe",
  "AsOf2017-12-31"
];
let pattern = /\d{4}\-\d{2}\-\d{2}/g;
strings.forEach((s) => {
  console.log(s.match(pattern));
});

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, will check this out!
Thanks I ran the snippet and I see the values as expected, thank you!!!
1

Remove word-boundary \b

var regex =  /\d{4}-\d{2}-\d{2}/g

Demo

var str1 = "2017-01-01to2017-12-31_custom_EquityPurchaseAgreementMember_custom_FirstPaymentMember_currency_CNY";
var str2 = "From2016-01-01to2016-12-31";
var str3 = "From2017-01-01to2017-12-31_custom_EquityPurchaseAgreementMember_custom_FirstPaymentMember";
var str4 = "AsOf2017-12-31";

var regex =  /\d{4}-\d{2}-\d{2}/g

console.log( str1.match(regex) );
console.log( str2.match(regex) );
console.log( str3.match(regex) );
console.log( str4.match(regex) );

1 Comment

Thank you, this one helps as well!
0

You may use

/(?:^|\D)(\d{4}-\d{2}-\d{2})(?!\d)/g

Extract Group 1. See the regex demo.

Here, (?:^|\D) matches a start of a string (^) or (|) any char other than a digit (\D). The (?!\d) is a negative lookahead that fails the match if there is a digit immediately to the right of the current location.

JS Demo:

var strs = ["From2017-01-01to2017-12-31_custom_EquityPurchaseAgreementMember_custom_FirstPaymentMember_currency_CNY","From2016-01-01to2016-12-31", "From2017-01-01to2017-12-31_custom_EquityPurchaseAgreementMember_custom_FirstPaymentMember", "AsOf2017-12-31" ];
var rx = /(?:^|\D)(\d{4}-\d{2}-\d{2})(?!\d)/g;
for (var s of strs) {
  console.log(s);
  var res=[], m;
  while (m=rx.exec(s)) {
     res.push(m[1]);
  }
  console.log(res);
}

With the browsers supporting ECMA 2018 standard (that supports lookbehinds),

/(?<!\d)\d{4}-\d{2}-\d{2}(?!\d)/g

See another regex demo. The (?<!\d) is a negative lookbehind that fails the match if there is a digit immediately to the left of the current location.

JS Demo:

var strs = ["From2017-01-01to2017-12-31_custom_EquityPurchaseAgreementMember_custom_FirstPaymentMember_currency_CNY","From2016-01-01to2016-12-31", "From2017-01-01to2017-12-31_custom_EquityPurchaseAgreementMember_custom_FirstPaymentMember", "AsOf2017-12-31" ];
var rx = /(?<!\d)\d{4}-\d{2}-\d{2}(?!\d)/g;
for (var s of strs) {
  console.log(s, "=>", s.match(rx));
}

Comments

0

the date values can be 1-1-2015 or 01-25-2015 so to make your regex more general you should use this regex

var regex = /([0-9]+-[0-9]+-[0-9]+)/g

and you can use this website to test your regex it's awesome and can generate the code for some languages too regex101

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.