1

I've got a pdf file turned into a huge string of over 1,000,000 characters. There are dates in the string in the format dd/mm/yyyy. I want to split the string by dates into smaller ones. I tried following:

var sectioned = hugeString.split(/^(0?[1-9]|[12][0-9]|3[01])[\/](0?[1-9]|1[012])[\/\-]\d{4}$/g);

But it's not working. I also tried hugeString.match(), but no good result there.

Is it even possible to accomplish this by string functions or should I think of a different approach?

String snippet:

....Section: 2 Interpretation E.R. 2 of 2012 02/08/2012 .....

2
  • Remove the ^ and $ anchors from that regex. They are needed when you want a string to be a date in its totality, but not when the string is allowed to have other text surrounding it. Commented Nov 9, 2016 at 13:04
  • 1
    Try .split(/(?:0?[1-9]|[12][0-9]|3[01])[\/-](?:0?[1-9]|1[012])[\/-]\d{4}/) - remove anchors, g modifier and use non-capturing groups. Wrap in (?=PATTERN HERE) if you need to split keeping the dates in the split chunks. Commented Nov 9, 2016 at 13:04

1 Answer 1

1

You may remove anchors, g modifier (it is redundant) and use non-capturing groups to avoid dates being output as well in the results. Wrap in (?=PATTERN HERE) if you need to split keeping the dates in the split chunks. However, if you prefer this approach, please make sure there are no optional 0s in the pattern at the beginning, or you might get redundant elements in the result.

var s = "....Section: 2 Interpretation E.R. 2 of 2012      02/08/2012 ..... ";
var res = s.split(/(?:0?[1-9]|[12][0-9]|3[01])[\/-](?:0?[1-9]|1[012])[\‌/-]\d{4}/);
console.log(res);
res = s.split(/(?=(?:0[1-9]|[12][0-9]|3[01])[\/-](?:0[1-9]|1[012])[\‌/-]\d{4})/);
console.log(res);

Note you also had a [\/] subpattern without - in the pattern while the other separator character class contained both chars. I suggest using [\/-] in both cases.

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

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.