Remove elements from a subgroup
I have the following string, I need to santize the zero elements inside the array, I must use regex for some reasons, because the scripting language I'm using I can't serialize/deserialize I have only a regex engine.
String:
{
"admins": [0, 148, 10, 0, 0, 0, 0, 0, 0, 0, 0],
"auth": "30639096bfe4ec4b9f17696ef1d02b9a",
}
and output a string like:
{
"admins": [148],
"auth": "30639096bfe4ec4b9f17696ef1d02b9a",
}
or eventually, if are all zero:
{
"admins": ,
"auth": "30639096bfe4ec4b9f17696ef1d02b9a",
}
Currently my pattern is:
(?<=admins":[[:space:]][)(.*(,[[:space:]]0))(?=])
I can't find the correct way to remove the subgroup data, actually I'm here: https://regex101.com/r/4yRSCn/1
regex regex-group
|
show 3 more comments
I have the following string, I need to santize the zero elements inside the array, I must use regex for some reasons, because the scripting language I'm using I can't serialize/deserialize I have only a regex engine.
String:
{
"admins": [0, 148, 10, 0, 0, 0, 0, 0, 0, 0, 0],
"auth": "30639096bfe4ec4b9f17696ef1d02b9a",
}
and output a string like:
{
"admins": [148],
"auth": "30639096bfe4ec4b9f17696ef1d02b9a",
}
or eventually, if are all zero:
{
"admins": ,
"auth": "30639096bfe4ec4b9f17696ef1d02b9a",
}
Currently my pattern is:
(?<=admins":[[:space:]][)(.*(,[[:space:]]0))(?=])
I can't find the correct way to remove the subgroup data, actually I'm here: https://regex101.com/r/4yRSCn/1
regex regex-group
2
Is it not a json ?
– splash58
Dec 29 '18 at 11:02
So, is it a .NET regex engine? If yes, why use regex101 that does not fully support it?
– Wiktor Stribiżew
Dec 29 '18 at 11:07
1
Try(?<="admins":s*[[^]*?)s*,?s*b0b,?s*
, see demo
– Wiktor Stribiżew
Dec 29 '18 at 11:12
2
@WiktorStribiżew You loss comma in case...10, 0, 5,...
– splash58
Dec 29 '18 at 11:15
1
Then(?<="admins":s*[[^]*?)(?:(?<=[)0(?:,s*0b)*,?|s*,s*0bs*)
– Wiktor Stribiżew
Dec 29 '18 at 11:24
|
show 3 more comments
I have the following string, I need to santize the zero elements inside the array, I must use regex for some reasons, because the scripting language I'm using I can't serialize/deserialize I have only a regex engine.
String:
{
"admins": [0, 148, 10, 0, 0, 0, 0, 0, 0, 0, 0],
"auth": "30639096bfe4ec4b9f17696ef1d02b9a",
}
and output a string like:
{
"admins": [148],
"auth": "30639096bfe4ec4b9f17696ef1d02b9a",
}
or eventually, if are all zero:
{
"admins": ,
"auth": "30639096bfe4ec4b9f17696ef1d02b9a",
}
Currently my pattern is:
(?<=admins":[[:space:]][)(.*(,[[:space:]]0))(?=])
I can't find the correct way to remove the subgroup data, actually I'm here: https://regex101.com/r/4yRSCn/1
regex regex-group
I have the following string, I need to santize the zero elements inside the array, I must use regex for some reasons, because the scripting language I'm using I can't serialize/deserialize I have only a regex engine.
String:
{
"admins": [0, 148, 10, 0, 0, 0, 0, 0, 0, 0, 0],
"auth": "30639096bfe4ec4b9f17696ef1d02b9a",
}
and output a string like:
{
"admins": [148],
"auth": "30639096bfe4ec4b9f17696ef1d02b9a",
}
or eventually, if are all zero:
{
"admins": ,
"auth": "30639096bfe4ec4b9f17696ef1d02b9a",
}
Currently my pattern is:
(?<=admins":[[:space:]][)(.*(,[[:space:]]0))(?=])
I can't find the correct way to remove the subgroup data, actually I'm here: https://regex101.com/r/4yRSCn/1
regex regex-group
regex regex-group
edited Dec 29 '18 at 11:06
Irvin Dominin
asked Dec 29 '18 at 11:00
Irvin DomininIrvin Dominin
27.5k85491
27.5k85491
2
Is it not a json ?
– splash58
Dec 29 '18 at 11:02
So, is it a .NET regex engine? If yes, why use regex101 that does not fully support it?
– Wiktor Stribiżew
Dec 29 '18 at 11:07
1
Try(?<="admins":s*[[^]*?)s*,?s*b0b,?s*
, see demo
– Wiktor Stribiżew
Dec 29 '18 at 11:12
2
@WiktorStribiżew You loss comma in case...10, 0, 5,...
– splash58
Dec 29 '18 at 11:15
1
Then(?<="admins":s*[[^]*?)(?:(?<=[)0(?:,s*0b)*,?|s*,s*0bs*)
– Wiktor Stribiżew
Dec 29 '18 at 11:24
|
show 3 more comments
2
Is it not a json ?
– splash58
Dec 29 '18 at 11:02
So, is it a .NET regex engine? If yes, why use regex101 that does not fully support it?
– Wiktor Stribiżew
Dec 29 '18 at 11:07
1
Try(?<="admins":s*[[^]*?)s*,?s*b0b,?s*
, see demo
– Wiktor Stribiżew
Dec 29 '18 at 11:12
2
@WiktorStribiżew You loss comma in case...10, 0, 5,...
– splash58
Dec 29 '18 at 11:15
1
Then(?<="admins":s*[[^]*?)(?:(?<=[)0(?:,s*0b)*,?|s*,s*0bs*)
– Wiktor Stribiżew
Dec 29 '18 at 11:24
2
2
Is it not a json ?
– splash58
Dec 29 '18 at 11:02
Is it not a json ?
– splash58
Dec 29 '18 at 11:02
So, is it a .NET regex engine? If yes, why use regex101 that does not fully support it?
– Wiktor Stribiżew
Dec 29 '18 at 11:07
So, is it a .NET regex engine? If yes, why use regex101 that does not fully support it?
– Wiktor Stribiżew
Dec 29 '18 at 11:07
1
1
Try
(?<="admins":s*[[^]*?)s*,?s*b0b,?s*
, see demo– Wiktor Stribiżew
Dec 29 '18 at 11:12
Try
(?<="admins":s*[[^]*?)s*,?s*b0b,?s*
, see demo– Wiktor Stribiżew
Dec 29 '18 at 11:12
2
2
@WiktorStribiżew You loss comma in case
...10, 0, 5,...
– splash58
Dec 29 '18 at 11:15
@WiktorStribiżew You loss comma in case
...10, 0, 5,...
– splash58
Dec 29 '18 at 11:15
1
1
Then
(?<="admins":s*[[^]*?)(?:(?<=[)0(?:,s*0b)*,?|s*,s*0bs*)
– Wiktor Stribiżew
Dec 29 '18 at 11:24
Then
(?<="admins":s*[[^]*?)(?:(?<=[)0(?:,s*0b)*,?|s*,s*0bs*)
– Wiktor Stribiżew
Dec 29 '18 at 11:24
|
show 3 more comments
1 Answer
1
active
oldest
votes
Since you are using a regex with .NET regex engine, you may leverage its infinite-length lookbehind pattern feature.
(?<="admins":s*[[^]*?)(?:(?<=[)0(?:,s*0b)*,?|s*,s*0bs*)
See the .NET regex demo.
Output:
Details
(?<="admins":s*[[^]*?)
- since you only want to remove0
s inside square brackets after"admins":[
substring, this positive lookebhind is meant to check that condition, the current position must be immediately preceded with:
"admins":
- a literal substring
s*
- 0+ whitespaces
[
- a[
char
[^]*?
- 0 or more chars other than[
and]
, as few as possible
(?:
- start of an alternation group:
(?<=[)
- a[
must immediately precede the current location
0
- a zero
(?:,s*0b)*
- zero or more repetitions of,
, 0+ whitespaces,0
and a word boundary
,?
- an optional comma
|
- or
s*,s*
- a comma enclosed with optional 0+ whitespaces
0
- a zero
b
- a word boundary
s*
- 0+ whitespaces
)
- end of an alternation group
This alternation group is necessary to differentiate between zeros at the start of the bracketed substring and those after, so that the commas and whitespace is removed consistently.
1
Great answer and explaination
– Irvin Dominin
Dec 29 '18 at 17:26
Could you explain[^]*?
part - I really don't get it.
– JohnyL
Dec 29 '18 at 18:44
@JohnyL[^...]
is a negated character class, it matches any char that is not a char defined in the class. Since]
and[
are inside the negated character class,[^]
matches a char other than[
and]
. The*?
is a lazy quantifier, that makes the pattern it modifies match 0 or more chars, as few as possible.
– Wiktor Stribiżew
Dec 29 '18 at 19:00
@WiktorStribiżew No, I know this. I meant why did you use it? What was the goal of it?
– JohnyL
Dec 29 '18 at 19:03
@JohnyL Make sure we match all the 0 numbers between square brackets.
– Wiktor Stribiżew
Dec 29 '18 at 19:11
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53968918%2fremove-elements-from-a-subgroup%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Since you are using a regex with .NET regex engine, you may leverage its infinite-length lookbehind pattern feature.
(?<="admins":s*[[^]*?)(?:(?<=[)0(?:,s*0b)*,?|s*,s*0bs*)
See the .NET regex demo.
Output:
Details
(?<="admins":s*[[^]*?)
- since you only want to remove0
s inside square brackets after"admins":[
substring, this positive lookebhind is meant to check that condition, the current position must be immediately preceded with:
"admins":
- a literal substring
s*
- 0+ whitespaces
[
- a[
char
[^]*?
- 0 or more chars other than[
and]
, as few as possible
(?:
- start of an alternation group:
(?<=[)
- a[
must immediately precede the current location
0
- a zero
(?:,s*0b)*
- zero or more repetitions of,
, 0+ whitespaces,0
and a word boundary
,?
- an optional comma
|
- or
s*,s*
- a comma enclosed with optional 0+ whitespaces
0
- a zero
b
- a word boundary
s*
- 0+ whitespaces
)
- end of an alternation group
This alternation group is necessary to differentiate between zeros at the start of the bracketed substring and those after, so that the commas and whitespace is removed consistently.
1
Great answer and explaination
– Irvin Dominin
Dec 29 '18 at 17:26
Could you explain[^]*?
part - I really don't get it.
– JohnyL
Dec 29 '18 at 18:44
@JohnyL[^...]
is a negated character class, it matches any char that is not a char defined in the class. Since]
and[
are inside the negated character class,[^]
matches a char other than[
and]
. The*?
is a lazy quantifier, that makes the pattern it modifies match 0 or more chars, as few as possible.
– Wiktor Stribiżew
Dec 29 '18 at 19:00
@WiktorStribiżew No, I know this. I meant why did you use it? What was the goal of it?
– JohnyL
Dec 29 '18 at 19:03
@JohnyL Make sure we match all the 0 numbers between square brackets.
– Wiktor Stribiżew
Dec 29 '18 at 19:11
|
show 1 more comment
Since you are using a regex with .NET regex engine, you may leverage its infinite-length lookbehind pattern feature.
(?<="admins":s*[[^]*?)(?:(?<=[)0(?:,s*0b)*,?|s*,s*0bs*)
See the .NET regex demo.
Output:
Details
(?<="admins":s*[[^]*?)
- since you only want to remove0
s inside square brackets after"admins":[
substring, this positive lookebhind is meant to check that condition, the current position must be immediately preceded with:
"admins":
- a literal substring
s*
- 0+ whitespaces
[
- a[
char
[^]*?
- 0 or more chars other than[
and]
, as few as possible
(?:
- start of an alternation group:
(?<=[)
- a[
must immediately precede the current location
0
- a zero
(?:,s*0b)*
- zero or more repetitions of,
, 0+ whitespaces,0
and a word boundary
,?
- an optional comma
|
- or
s*,s*
- a comma enclosed with optional 0+ whitespaces
0
- a zero
b
- a word boundary
s*
- 0+ whitespaces
)
- end of an alternation group
This alternation group is necessary to differentiate between zeros at the start of the bracketed substring and those after, so that the commas and whitespace is removed consistently.
1
Great answer and explaination
– Irvin Dominin
Dec 29 '18 at 17:26
Could you explain[^]*?
part - I really don't get it.
– JohnyL
Dec 29 '18 at 18:44
@JohnyL[^...]
is a negated character class, it matches any char that is not a char defined in the class. Since]
and[
are inside the negated character class,[^]
matches a char other than[
and]
. The*?
is a lazy quantifier, that makes the pattern it modifies match 0 or more chars, as few as possible.
– Wiktor Stribiżew
Dec 29 '18 at 19:00
@WiktorStribiżew No, I know this. I meant why did you use it? What was the goal of it?
– JohnyL
Dec 29 '18 at 19:03
@JohnyL Make sure we match all the 0 numbers between square brackets.
– Wiktor Stribiżew
Dec 29 '18 at 19:11
|
show 1 more comment
Since you are using a regex with .NET regex engine, you may leverage its infinite-length lookbehind pattern feature.
(?<="admins":s*[[^]*?)(?:(?<=[)0(?:,s*0b)*,?|s*,s*0bs*)
See the .NET regex demo.
Output:
Details
(?<="admins":s*[[^]*?)
- since you only want to remove0
s inside square brackets after"admins":[
substring, this positive lookebhind is meant to check that condition, the current position must be immediately preceded with:
"admins":
- a literal substring
s*
- 0+ whitespaces
[
- a[
char
[^]*?
- 0 or more chars other than[
and]
, as few as possible
(?:
- start of an alternation group:
(?<=[)
- a[
must immediately precede the current location
0
- a zero
(?:,s*0b)*
- zero or more repetitions of,
, 0+ whitespaces,0
and a word boundary
,?
- an optional comma
|
- or
s*,s*
- a comma enclosed with optional 0+ whitespaces
0
- a zero
b
- a word boundary
s*
- 0+ whitespaces
)
- end of an alternation group
This alternation group is necessary to differentiate between zeros at the start of the bracketed substring and those after, so that the commas and whitespace is removed consistently.
Since you are using a regex with .NET regex engine, you may leverage its infinite-length lookbehind pattern feature.
(?<="admins":s*[[^]*?)(?:(?<=[)0(?:,s*0b)*,?|s*,s*0bs*)
See the .NET regex demo.
Output:
Details
(?<="admins":s*[[^]*?)
- since you only want to remove0
s inside square brackets after"admins":[
substring, this positive lookebhind is meant to check that condition, the current position must be immediately preceded with:
"admins":
- a literal substring
s*
- 0+ whitespaces
[
- a[
char
[^]*?
- 0 or more chars other than[
and]
, as few as possible
(?:
- start of an alternation group:
(?<=[)
- a[
must immediately precede the current location
0
- a zero
(?:,s*0b)*
- zero or more repetitions of,
, 0+ whitespaces,0
and a word boundary
,?
- an optional comma
|
- or
s*,s*
- a comma enclosed with optional 0+ whitespaces
0
- a zero
b
- a word boundary
s*
- 0+ whitespaces
)
- end of an alternation group
This alternation group is necessary to differentiate between zeros at the start of the bracketed substring and those after, so that the commas and whitespace is removed consistently.
answered Dec 29 '18 at 17:24
Wiktor StribiżewWiktor Stribiżew
312k16132207
312k16132207
1
Great answer and explaination
– Irvin Dominin
Dec 29 '18 at 17:26
Could you explain[^]*?
part - I really don't get it.
– JohnyL
Dec 29 '18 at 18:44
@JohnyL[^...]
is a negated character class, it matches any char that is not a char defined in the class. Since]
and[
are inside the negated character class,[^]
matches a char other than[
and]
. The*?
is a lazy quantifier, that makes the pattern it modifies match 0 or more chars, as few as possible.
– Wiktor Stribiżew
Dec 29 '18 at 19:00
@WiktorStribiżew No, I know this. I meant why did you use it? What was the goal of it?
– JohnyL
Dec 29 '18 at 19:03
@JohnyL Make sure we match all the 0 numbers between square brackets.
– Wiktor Stribiżew
Dec 29 '18 at 19:11
|
show 1 more comment
1
Great answer and explaination
– Irvin Dominin
Dec 29 '18 at 17:26
Could you explain[^]*?
part - I really don't get it.
– JohnyL
Dec 29 '18 at 18:44
@JohnyL[^...]
is a negated character class, it matches any char that is not a char defined in the class. Since]
and[
are inside the negated character class,[^]
matches a char other than[
and]
. The*?
is a lazy quantifier, that makes the pattern it modifies match 0 or more chars, as few as possible.
– Wiktor Stribiżew
Dec 29 '18 at 19:00
@WiktorStribiżew No, I know this. I meant why did you use it? What was the goal of it?
– JohnyL
Dec 29 '18 at 19:03
@JohnyL Make sure we match all the 0 numbers between square brackets.
– Wiktor Stribiżew
Dec 29 '18 at 19:11
1
1
Great answer and explaination
– Irvin Dominin
Dec 29 '18 at 17:26
Great answer and explaination
– Irvin Dominin
Dec 29 '18 at 17:26
Could you explain
[^]*?
part - I really don't get it.– JohnyL
Dec 29 '18 at 18:44
Could you explain
[^]*?
part - I really don't get it.– JohnyL
Dec 29 '18 at 18:44
@JohnyL
[^...]
is a negated character class, it matches any char that is not a char defined in the class. Since ]
and [
are inside the negated character class, [^]
matches a char other than [
and ]
. The *?
is a lazy quantifier, that makes the pattern it modifies match 0 or more chars, as few as possible.– Wiktor Stribiżew
Dec 29 '18 at 19:00
@JohnyL
[^...]
is a negated character class, it matches any char that is not a char defined in the class. Since ]
and [
are inside the negated character class, [^]
matches a char other than [
and ]
. The *?
is a lazy quantifier, that makes the pattern it modifies match 0 or more chars, as few as possible.– Wiktor Stribiżew
Dec 29 '18 at 19:00
@WiktorStribiżew No, I know this. I meant why did you use it? What was the goal of it?
– JohnyL
Dec 29 '18 at 19:03
@WiktorStribiżew No, I know this. I meant why did you use it? What was the goal of it?
– JohnyL
Dec 29 '18 at 19:03
@JohnyL Make sure we match all the 0 numbers between square brackets.
– Wiktor Stribiżew
Dec 29 '18 at 19:11
@JohnyL Make sure we match all the 0 numbers between square brackets.
– Wiktor Stribiżew
Dec 29 '18 at 19:11
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53968918%2fremove-elements-from-a-subgroup%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Is it not a json ?
– splash58
Dec 29 '18 at 11:02
So, is it a .NET regex engine? If yes, why use regex101 that does not fully support it?
– Wiktor Stribiżew
Dec 29 '18 at 11:07
1
Try
(?<="admins":s*[[^]*?)s*,?s*b0b,?s*
, see demo– Wiktor Stribiżew
Dec 29 '18 at 11:12
2
@WiktorStribiżew You loss comma in case
...10, 0, 5,...
– splash58
Dec 29 '18 at 11:15
1
Then
(?<="admins":s*[[^]*?)(?:(?<=[)0(?:,s*0b)*,?|s*,s*0bs*)
– Wiktor Stribiżew
Dec 29 '18 at 11:24