Wrap capture group in quotes if it contains non-digit characters?

Viewed 36

I can't seem to find any answers on how to wrap the resulting capture group in quotes when using regex.

If I got this:

Abcd abcd 12345

Abcd 1234 12345

I want this:

"Abcd" "abcd" 12345

or this:

"Abcd" 1234 12345

How can I do something like this:

"$1" (?$2=[^0-9] \"$2\" | $2) in the replace input?

Or in other words: I need to check if the result of the capture group (the result of the match) is of one or the other type and then wrap in quotation marks or not.

Read as: "if capture group contains non digit characters replace it with the value wrapped in quotation marks, otherwise replace with the number".

I can't seem to figure out how to do the actual IF / THEN in the result input – or understand how it would work in the reg itself (lacking a mind map here).


Additional info:

I've got these two variations (notice the different second group):

string-digits digits digits datetime datetime string string digits-digits string

string-digits string-digits-whitespace digits datetime datetime string string string-digits string

So far I'm trying to use this regex:

(.+) (THIS IS THE TRICKY PART) ([0-9]+) ([0-9]+/[0-9]+/[0-9]+ [0-9]+:[0-9]+) [0-9]+/[0-9]+/[0-9]+ [0-9]+:[0-9]+ (.+) (.+) (.+-[0-9]+) (.+)

I need to create this:

{
    "unique-key" : "$1",
    "unique-key" : $2,      <-- this second capture group should be wrapped in quotes if the value is not an integer.
    "unique-key" : $3,
    "unique-key" : "$4",
    "unique-key" : "$5",
    "unique-key" : "$6",
    "unique-key" : "$6",
    "unique-key" : "$7",
    "unique-key" : "$8"
}
1 Answers

If I understand it correctly - numbers should be left without changes. So, only words containing non-digit characters should be surrounded with quotes.

Then, the pattern may look like:

([^\s]*[^\d\s]+[^\s]*)

What is matched by this pattern should be replaced with "$1"

Related