1

So, I have a spinEdit that should display the year and month in this format yyyyMM. I am using RegEx to mask the value to that format but when I want to increment from say 201212 to 201301, it fails and displays 20121. The RegEx I am using looks like this

([0-9][0-9][0-9][0-9])(0[1-9])|(1[0-2])

The issue is that incrementing the value (add 1 to month) isn't incrementing the year field when the month is at 12. The same happens in reverse where decreasing the value (minus 1 month) isn't decreasing the year, 201301 - 1 takes it to 2013. Is there a way to fix this using just RegEx?

GoZoner
  • 59,252
  • 19
  • 87
  • 137
5tar-Kaster
  • 892
  • 2
  • 11
  • 29
  • 1
    I am not familiar with devexpress, anyway are you sure about your regex? I would better use something like `/(19|20)([0-9][0-9])(0[1-9]|1[0-2])/`. And what do you mean by fixing this "using just RegEx"? – davide Aug 21 '13 at 12:09
  • Thanks for that tip however that beginning doesn't really alter much and the problem still remains – 5tar-Kaster Aug 21 '13 at 12:19
  • 1
    Yes, I know. I would better use some if-checks to see if what you captured with the month-group is equal to 12 (for addition) or to 01 (for subtraction) and similarly for the year. – davide Aug 21 '13 at 12:26
  • 2
    Regex is all about matching (and in some cases replacing). Us, humans see "2012" as a number, regex sees it as a mere group of digits, or better said as a mere sequence of characters. It can't increment it, it can't decrement it. That's how it is. Unless you can match those characters and use a callback to increment it, there is NO WAY that you could do that with only regex. Of course you may see some crazy stuff like [this](http://en.ricbit.com/2013/04/arithmetic-with-regexps.html) or [this](http://stackoverflow.com/a/18262967/) (scroll to regex solution). – HamZa Aug 21 '13 at 12:32
  • @HamZa Thanks, I was trying to take the method out of this simple problem, because I just wanted to see if it was possible. Those regex things are a little advanced but thanks anyways. – 5tar-Kaster Aug 21 '13 at 12:47
  • @5tar-Kaster Please have a look at the solution? – Juto Sep 02 '13 at 16:51
  • @Juto I said in the question "using just regex" so the answer isn't actually and answer to the question. Because of the unique and rather annoying way my application is done, I was hoping to use only regex instead of events to handle this part. – 5tar-Kaster Sep 04 '13 at 06:21
  • Fair enough. Thanks for letting me know. – Juto Sep 04 '13 at 06:59

2 Answers2

0

I think it is possible, but not fully regex solution, you need to have linux and bash available (personally I find the date function in bash ve) I had to get the date formats (string) in a filename and compare it to a date in a script. Below is the code snippet:

#!/bin/bash

#yyyymm you got after regex
inputdate = 201307 
#value you want to subtract
x = 8
#outputdate should return you 201211    
outputdate = $(date -d "$inputdate01 -$x month" +"%Y%m") 
Juto
  • 1,086
  • 1
  • 11
  • 23
0

I believe there may be a way, however that is far to complicated for its worth in a practical situation. So by keeping things simple, it is not possible.

5tar-Kaster
  • 892
  • 2
  • 11
  • 29