0

I struggling to find the correct LUA code to detect if two dates appear after each other.

I have something similar which detects two keywords, but it's not working on my dates.

Here's the LUA code I have so far:

(%a+) %- %1$

Cheers,

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
greenage
  • 399
  • 3
  • 13
  • 1
    Can you give an example of what you want to match? Also, what language are you working in? – Elmar Peise Mar 15 '17 at 17:32
  • 1
    Please provide some examples of the text you are checking and the expected result. – Sam Mar 15 '17 at 17:32
  • It seems you want to match repeat the same pattern with `%1` - no, this `%1` matches the same alpha characters matched with `(%a+)`. Try removing `$` end of string anchor. Please provide sample strings you are trying to match. BTW, what you are using is not regex, these are Lua patterns. – Wiktor Stribiżew Mar 15 '17 at 17:36
  • I absolutely wouldn't use regex for this. It will be hard to get right, especially if it's possible for a date to be duplicated more than once. What tools/languages do you have at your disposal? – jpmc26 Mar 15 '17 at 17:51
  • Thanks all, sure, here's what I'm trying to match: – greenage Mar 15 '17 at 18:01
  • 2017-03-15 - 2017-03-15 – greenage Mar 15 '17 at 18:01
  • 1
    Check http://ideone.com/1F1L9s, try `'^(%d+%-%d+%-%d+) %- %1$'` or `'(%d+%-%d+%-%d+) %- %1'` (if you want to match shorter substrings inside a longer string). – Wiktor Stribiżew Mar 15 '17 at 18:20

1 Answers1

2

Your main trouble here is that you want to match a date like 2017-03-19 with %a+ pattern. %a matches a letter, %a+ matches 1 or more letters.

You need to replace this pattern with a more precise one, like %d+%-%d+%-%d+ or %d%d%d%d%-%d%d%-%d%d:

'(%d+%-%d+%-%d+) %- %1'

where %d matches a digit.

Now, if you want to match a whole string like this, you need to enclose the pattern with ^ and $ anchors.

'^(%d+%-%d+%-%d+) %- %1$'

If you want to add word boundaries,

 '%f[%d](%d+%-%d+%-%d+) %- %1%f[%D]'
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563