I have a problem with matching text in Vim buffers. I have a specific form of dates in my text and often need to test whether the text at cursor matches the date pattern.
Here are some examples of the formatted dates, which may be preceded or followed by other text on the same line:
<2011-10-13 Wed>
[2011-10-13 Wed]
<2011-10-13 Wed +1>
[2011-10-13 Wed +1]
<2011-10-13 Wed 10:30>
[2011-10-13 Wed 10:30]
<2011-10-13 Wed 10:30-11:30>
[2011-10-13 Wed 10:30-11:30]
<2011-10-13 Wed 10:30 +1w>
[2011-10-13 Wed 10:30 +1w]
I have some code that tests at cursor position to see whether cursor is on a date, and if so what the date text is, but what I’m doing seems kind of clumsy.
Any comments on what most efficient function for returning the date under cursor (or empty string if not on a date)? (I would post my code but will refrain for now both (1) out of embarrassment and (2) to avoid polluting your minds by suggesting a particular approach to the problem.)
Thanks for any tips.
The function
IsOverDate()clears the registersaandband stores in the respective registers the text under the cursor which is inside<and>and<and>, including the brackets. Then, it gets the value from the registersaandband sends it to the functionCheckDate()which checks if the expression matches the date pattern (I have based myself in your samples and made some assumptions to build the pattern).The
CheckDate()function returns true only if the expression matches the date pattern. The functionIsOverDate()returns the date under the cursor (with the brackets) or an empty string if the cursor is not over a date.I hope it suits.