2

I am cleaning a dataset using Google Refine. I have one column with dates in the mm/dd/yyyy format. I want to create a new column in which mm/dd/yyyy is replaced by yyyy only.

I have tried

value.replace(/.+(\d\d\d\d)\*/, /$1/)

and what showed up was

Error: replace expects 3 strings, or 1 string, 1 regex, and 1 string

Why does this error show up? Thank you for helping a beginner!

AnnaGo
  • 23
  • 2

3 Answers3

1

If the values are just dates in that regular format, the easy solution is:

value.split('/')[2]

if you need to pluck the date out of the middle of a long string or just have a warped desire to mess with regex :-) then you can use

value.replace(/([1-9]|0[1-9]|[12][0-9]|3[01])\D([1-9]|0[1-9]|1[012])\D(19[0-9][0-9]|20[0-9][0-9])/, "$3")

BTW, there are lots of canned regexes on the web that you can just search for rather than recreating them yourself which is what I did here. I wouldn't have made it anywhere near that complex/specific. You should adjust it to your needs, depending on how strict/liberal you want your matching.

Tom Morris
  • 10,490
  • 32
  • 53
0

I cannot reproduce the problem, but your regex is incorrect, try this

System.out.println("01/01/2012".replaceAll("(\\d+/){2}(.+)", "$2"));

Note that String.replace() does not work with regex. Use String.replaceAll() or String.replaceFirst() instead.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • Google Refine does not recognizeSystem.out.println, and when I tried replaceChars("(\\d+/){2}(.+)", "$2")), the forward slashes between the numbers disappeared but nothing else changed. – AnnaGo Dec 02 '12 at 04:57
0

See the comma in your replace, the method over-loadings for that class expect either 3 strings or 1 string meaning no comma or two commas separating arguments to .replace() method.

nicephotog
  • 11
  • 1