UKC

Regexr help

New Topic
This topic has been archived, and won't accept reply postings.
 Climber_Bill 27 Aug 2014
Hi,

I need some help with a regular expression in vi.

Have thousands of dates in the format yyyy-mm-dd. I need to put double quotes around each of them to make "yyyy-mm-dd". Can't work out what the regexr should be to do it. Finding the pattern is ok using /([0-9]{4}-[0-9]{2}-[0-9]{2})/g but can't then get the quotes around the matched pattern

Anybody got any ideas?

Thanks in advance.
 Bob 27 Aug 2014
In reply to Richard White:
Place the different parts of the regex in parentheses to group them, then use $1 for the first match, $2 for the second, etc.

So your replacement is /([0-9]{4})-([0-9]{2})-([0-9]{2})/"$1"-"$2"-"$3"/g

Slightly easier to read is:

/(d+)-(d+)-(d+)/"$1"-"$2"-"$3"/g

Though that doesn't limit the number of digits in each group so you could have 12334567-9-21 as a valid input.
Post edited at 12:46
 Jack B 27 Aug 2014
In reply to Bob:
Er... I don't think that's what he wanted? Won't that produce something like "yyyy"-"mm"-"dd"?

/([0-9]{4}-[0-9]{2}-[0-9]{2})/"$1"/g

Maybe. Haven't done this in ages.
Post edited at 12:48
interdit 27 Aug 2014
In reply to Richard White:

:%s/d{4}-d{2}-d{2}/""/g

http://vimregex.com/#substitute
 Bob 27 Aug 2014
In reply to Jack B:

Oops, yes you're right. I misread the request. You need the grouping operator around the whole regex then do "$1" to quote it.
OP Climber_Bill 27 Aug 2014
In reply to All:

Thanks for your help. Got it working using the provided examples.

Good to see the art of the regular expression is not dead yet!

Rich.


New Topic
This topic has been archived, and won't accept reply postings.
Loading Notifications...