Reputation: 4786
I want to turn this string;
0000,0767,"078", 0785, "0723",23487, 345 , 07334
Into this string;
"0000","0767","078", "0785", "0723","23487", "345" , "07334"
This is the closest I can get to it, I always get a bit confused with negative lookups and such when it comes to regex.
[^"\d,]?(\d+)
and a replace with "$1"
- https://regex101.com/r/qVQYA7/1
unfortunately this results in double wrapped quotes for integers that already have double quotes around them, like so;
"0000","0767",""078"","0785", ""0723"","23487","345" ,"07334"
The pseudo logic is; look for any integers that don't already have double quotes around them, and add double quotes. Leave any spacing in between commas as is.
Upvotes: 4
Views: 4348
Reputation: 92904
Let's say that in your case each sequence of numbers should be followed by comma ,
or positioned at the end of the string.
Use the following regex:
(\d+)\s*?(?=,|$)
https://regex101.com/r/qVQYA7/3
Upvotes: 2
Reputation: 8413
You can simply search for "?(\d+)"?
and replace it with "$1"
.
If there are " present, they are matched but not contained in the group.
Upvotes: 5
Reputation: 786329
You can use lookarounds in your regex to avoid matching quoted numbers:
(?<!")(\b\d+\b)(?!")
RegEx Breakup:
(?<!") # Negative lookbehind to skip the match if previous character is "
( # captured group #1 open
\b # assert word boundary
\d+ # match 1 or more digits
\b # assert word boundary
) # captured group close
(?!") # Negative lookahead to skip the match if next character is "
If you're using PCRE then you can use this PCRE verb:
"\d+"(*SKIP)(*F)|(\d+)
Upvotes: 2