Reputation: 2304
So I use a lot of regex to format SQL.
I'm trying to match all quotes surrounding numeric values (INT) so I can remove them.
I use this to match numerics in qoutes:
(?<=(['"])\b)(?:(?!\1|\\)[0-9]|\\.)*(?=\1)
Playing with this so far but no luck yet:
'(?=[0-9](?='))*
What i'm trying to say is look ahead infinity, matching anything that is a number unless it is quote then accept then match.
Any regex ninja's out there can help put me on the path?
Here's an example string:
'2018-12-09 07:29:00.0000000', 'US', 'MI', 'Detroit', '48206', '505', '68.61.112.245', '0', 'Verizon'
I just want to match the '
around 48206
, 505
, and 0
so I can strip them.
To be safe lets assume there are other characters as well that could appear in the test string. ie - its not really feasible to say just match anything that's no a dash a letter or a dot, etc. Also the question is language-agnostic so any applicable language is fine -- JavaScript, Python, Java, etc.
Upvotes: 1
Views: 1074
Reputation: 163362
You could capture a single or a double quote as in your first regex in a capturing group and then capture the digits in betweenin group 2 and finally use a backreference to group 1
In the replacement, use the second capturing group $2
for example
(['"])(\d+)\1
Explanation
(['"])
Capture ' or " in a capturing group(\d+)
Capture 1+ digits in a group\1
Backreference to group 1Result
''2018-12-09 07:29:00.0000000', 'US', 'MI', 'Detroit', 48206, 505, '68.61.112.245', 0, 'Verizon''
Upvotes: 1
Reputation: 43910
.split().join()
Chain.split()
can use RegEx such as this:
/'\b([0-9]+?)\b'/
Literal match single straight quote: '
Meta sequence word boundary sets the beginning of a word/number: \b
Capture group: (
class range: [
of any digit: 0-9]
Match at least once and continue to do so until the next word border is reached and a literal straight single quote: )+?\b'
Since .split()
iterates through the string a g
lobal flag isn't needed. .join('');
is chained to .split()
and the result is back to a string from am array.
var strA = `'2018-12-09 07:29:00.0000000', 'US', 'MI', 'Detroit', '48206', '505', '68.61.112.245', '0', 'Verizon'`;
var strB = strA.split(/'\b([0-9]+?)\b'/).join('');
console.log(strB);
Upvotes: 1
Reputation: 18357
You can select all such numbers using this regex,
'(\d+)'
And then replace it with \1
or $2
as per your language.
This will get rid of all quotes that are surrounding numbers.
Let me know if this works for you.
Also, as an alternative solution, if your regex engine supports ECMAScript 2018, then you can exploit variable length look behind and use this regex to select only quotes that surround a number,
'(?=\d+')|(?<='\d+)'
And replace it with empty string.
Make sure you check this demo in Chrome which supports it and not Mozilla which doesn't support it.
Upvotes: 1