Regex to search through a file for a set of parentheses with eight characters inside them, all 1s or 0s, with...
I'm trying to search through a file for a set of parentheses with eight characters inside them, all 1s or 0s, with at least one 1.
I currently am using the regex below, enumerating all possible such parentheses sets, i.e. brute forcing it.
Is there a better way to do this?
My regex:
(11111110)|(11111101)|(11111100)|(11111011)|(11111010)|(11111001)|(11111000)|
...|(11111111)
etc.
regex
add a comment |
I'm trying to search through a file for a set of parentheses with eight characters inside them, all 1s or 0s, with at least one 1.
I currently am using the regex below, enumerating all possible such parentheses sets, i.e. brute forcing it.
Is there a better way to do this?
My regex:
(11111110)|(11111101)|(11111100)|(11111011)|(11111010)|(11111001)|(11111000)|
...|(11111111)
etc.
regex
Yeah, usegrep -oP 'b(?=[01]{8}b)0*1[10]*'
– Wiktor Stribiżew
Nov 13 '18 at 19:10
See my answer below, I modified my comment as I did not realize you wanted to match these binary data inside parentheses.
– Wiktor Stribiżew
Nov 13 '18 at 19:34
add a comment |
I'm trying to search through a file for a set of parentheses with eight characters inside them, all 1s or 0s, with at least one 1.
I currently am using the regex below, enumerating all possible such parentheses sets, i.e. brute forcing it.
Is there a better way to do this?
My regex:
(11111110)|(11111101)|(11111100)|(11111011)|(11111010)|(11111001)|(11111000)|
...|(11111111)
etc.
regex
I'm trying to search through a file for a set of parentheses with eight characters inside them, all 1s or 0s, with at least one 1.
I currently am using the regex below, enumerating all possible such parentheses sets, i.e. brute forcing it.
Is there a better way to do this?
My regex:
(11111110)|(11111101)|(11111100)|(11111011)|(11111010)|(11111001)|(11111000)|
...|(11111111)
etc.
regex
regex
edited Nov 13 '18 at 21:15
ttarchala
2,1071929
2,1071929
asked Nov 13 '18 at 19:04
anonanon
183
183
Yeah, usegrep -oP 'b(?=[01]{8}b)0*1[10]*'
– Wiktor Stribiżew
Nov 13 '18 at 19:10
See my answer below, I modified my comment as I did not realize you wanted to match these binary data inside parentheses.
– Wiktor Stribiżew
Nov 13 '18 at 19:34
add a comment |
Yeah, usegrep -oP 'b(?=[01]{8}b)0*1[10]*'
– Wiktor Stribiżew
Nov 13 '18 at 19:10
See my answer below, I modified my comment as I did not realize you wanted to match these binary data inside parentheses.
– Wiktor Stribiżew
Nov 13 '18 at 19:34
Yeah, use
grep -oP 'b(?=[01]{8}b)0*1[10]*'
– Wiktor Stribiżew
Nov 13 '18 at 19:10
Yeah, use
grep -oP 'b(?=[01]{8}b)0*1[10]*'
– Wiktor Stribiżew
Nov 13 '18 at 19:10
See my answer below, I modified my comment as I did not realize you wanted to match these binary data inside parentheses.
– Wiktor Stribiżew
Nov 13 '18 at 19:34
See my answer below, I modified my comment as I did not realize you wanted to match these binary data inside parentheses.
– Wiktor Stribiżew
Nov 13 '18 at 19:34
add a comment |
3 Answers
3
active
oldest
votes
Use
grep -oP '((?=[01]{8}))0*1[10]*)' file
See the regex demo.
Details
-o
- outputs matches rather than matching lines
P
- enables PCRE regex engine
Pattern
(
- a(
char
(?=[01]{8}))
- a positive lookahead that requires eight0
or1
chars up to to a)
0*
- 0+ zeros
1
- a1
char
[10]*
- 0 or more zeros or ones
)
- a)
.
1
This works great too. Of course, as it is by Wiktor "The Great Regexer" :)
– Pushpesh Kumar Rajwanshi
Nov 13 '18 at 19:37
add a comment |
You may use this regex,
((?=[10]{8})(?=.*1.*).{8})
Explanation:
(
--> Matches a literal(
(starting brace)
(?=[10]{8})
--> Look ahead ensuring next eight characters are composed of zero and one only
(?=.*1.*)
--> Look ahead ensuring the presence of at least one '1' character
.{8}
--> matches exactly eight characters
)
--> Matches a literal)
(closing brace)
Demo
Thanks for your help, however that still finds the string (00000000) which I was hoping to avoid, as its the base case
– anon
Nov 13 '18 at 19:31
It doesn't match (00000000). Check this regex101.com/r/lr8haq/2
– Pushpesh Kumar Rajwanshi
Nov 13 '18 at 19:32
add a comment |
This regex could also work:
(?!(00000000))(d{8})
See Lookarounds (Usually) Want to be Anchored.
– Wiktor Stribiżew
Nov 13 '18 at 21:24
add a 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%2f53287890%2fregex-to-search-through-a-file-for-a-set-of-parentheses-with-eight-characters-in%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use
grep -oP '((?=[01]{8}))0*1[10]*)' file
See the regex demo.
Details
-o
- outputs matches rather than matching lines
P
- enables PCRE regex engine
Pattern
(
- a(
char
(?=[01]{8}))
- a positive lookahead that requires eight0
or1
chars up to to a)
0*
- 0+ zeros
1
- a1
char
[10]*
- 0 or more zeros or ones
)
- a)
.
1
This works great too. Of course, as it is by Wiktor "The Great Regexer" :)
– Pushpesh Kumar Rajwanshi
Nov 13 '18 at 19:37
add a comment |
Use
grep -oP '((?=[01]{8}))0*1[10]*)' file
See the regex demo.
Details
-o
- outputs matches rather than matching lines
P
- enables PCRE regex engine
Pattern
(
- a(
char
(?=[01]{8}))
- a positive lookahead that requires eight0
or1
chars up to to a)
0*
- 0+ zeros
1
- a1
char
[10]*
- 0 or more zeros or ones
)
- a)
.
1
This works great too. Of course, as it is by Wiktor "The Great Regexer" :)
– Pushpesh Kumar Rajwanshi
Nov 13 '18 at 19:37
add a comment |
Use
grep -oP '((?=[01]{8}))0*1[10]*)' file
See the regex demo.
Details
-o
- outputs matches rather than matching lines
P
- enables PCRE regex engine
Pattern
(
- a(
char
(?=[01]{8}))
- a positive lookahead that requires eight0
or1
chars up to to a)
0*
- 0+ zeros
1
- a1
char
[10]*
- 0 or more zeros or ones
)
- a)
.
Use
grep -oP '((?=[01]{8}))0*1[10]*)' file
See the regex demo.
Details
-o
- outputs matches rather than matching lines
P
- enables PCRE regex engine
Pattern
(
- a(
char
(?=[01]{8}))
- a positive lookahead that requires eight0
or1
chars up to to a)
0*
- 0+ zeros
1
- a1
char
[10]*
- 0 or more zeros or ones
)
- a)
.
edited Nov 13 '18 at 19:36
answered Nov 13 '18 at 19:30
Wiktor StribiżewWiktor Stribiżew
314k16133212
314k16133212
1
This works great too. Of course, as it is by Wiktor "The Great Regexer" :)
– Pushpesh Kumar Rajwanshi
Nov 13 '18 at 19:37
add a comment |
1
This works great too. Of course, as it is by Wiktor "The Great Regexer" :)
– Pushpesh Kumar Rajwanshi
Nov 13 '18 at 19:37
1
1
This works great too. Of course, as it is by Wiktor "The Great Regexer" :)
– Pushpesh Kumar Rajwanshi
Nov 13 '18 at 19:37
This works great too. Of course, as it is by Wiktor "The Great Regexer" :)
– Pushpesh Kumar Rajwanshi
Nov 13 '18 at 19:37
add a comment |
You may use this regex,
((?=[10]{8})(?=.*1.*).{8})
Explanation:
(
--> Matches a literal(
(starting brace)
(?=[10]{8})
--> Look ahead ensuring next eight characters are composed of zero and one only
(?=.*1.*)
--> Look ahead ensuring the presence of at least one '1' character
.{8}
--> matches exactly eight characters
)
--> Matches a literal)
(closing brace)
Demo
Thanks for your help, however that still finds the string (00000000) which I was hoping to avoid, as its the base case
– anon
Nov 13 '18 at 19:31
It doesn't match (00000000). Check this regex101.com/r/lr8haq/2
– Pushpesh Kumar Rajwanshi
Nov 13 '18 at 19:32
add a comment |
You may use this regex,
((?=[10]{8})(?=.*1.*).{8})
Explanation:
(
--> Matches a literal(
(starting brace)
(?=[10]{8})
--> Look ahead ensuring next eight characters are composed of zero and one only
(?=.*1.*)
--> Look ahead ensuring the presence of at least one '1' character
.{8}
--> matches exactly eight characters
)
--> Matches a literal)
(closing brace)
Demo
Thanks for your help, however that still finds the string (00000000) which I was hoping to avoid, as its the base case
– anon
Nov 13 '18 at 19:31
It doesn't match (00000000). Check this regex101.com/r/lr8haq/2
– Pushpesh Kumar Rajwanshi
Nov 13 '18 at 19:32
add a comment |
You may use this regex,
((?=[10]{8})(?=.*1.*).{8})
Explanation:
(
--> Matches a literal(
(starting brace)
(?=[10]{8})
--> Look ahead ensuring next eight characters are composed of zero and one only
(?=.*1.*)
--> Look ahead ensuring the presence of at least one '1' character
.{8}
--> matches exactly eight characters
)
--> Matches a literal)
(closing brace)
Demo
You may use this regex,
((?=[10]{8})(?=.*1.*).{8})
Explanation:
(
--> Matches a literal(
(starting brace)
(?=[10]{8})
--> Look ahead ensuring next eight characters are composed of zero and one only
(?=.*1.*)
--> Look ahead ensuring the presence of at least one '1' character
.{8}
--> matches exactly eight characters
)
--> Matches a literal)
(closing brace)
Demo
answered Nov 13 '18 at 19:28
Pushpesh Kumar RajwanshiPushpesh Kumar Rajwanshi
7,5282927
7,5282927
Thanks for your help, however that still finds the string (00000000) which I was hoping to avoid, as its the base case
– anon
Nov 13 '18 at 19:31
It doesn't match (00000000). Check this regex101.com/r/lr8haq/2
– Pushpesh Kumar Rajwanshi
Nov 13 '18 at 19:32
add a comment |
Thanks for your help, however that still finds the string (00000000) which I was hoping to avoid, as its the base case
– anon
Nov 13 '18 at 19:31
It doesn't match (00000000). Check this regex101.com/r/lr8haq/2
– Pushpesh Kumar Rajwanshi
Nov 13 '18 at 19:32
Thanks for your help, however that still finds the string (00000000) which I was hoping to avoid, as its the base case
– anon
Nov 13 '18 at 19:31
Thanks for your help, however that still finds the string (00000000) which I was hoping to avoid, as its the base case
– anon
Nov 13 '18 at 19:31
It doesn't match (00000000). Check this regex101.com/r/lr8haq/2
– Pushpesh Kumar Rajwanshi
Nov 13 '18 at 19:32
It doesn't match (00000000). Check this regex101.com/r/lr8haq/2
– Pushpesh Kumar Rajwanshi
Nov 13 '18 at 19:32
add a comment |
This regex could also work:
(?!(00000000))(d{8})
See Lookarounds (Usually) Want to be Anchored.
– Wiktor Stribiżew
Nov 13 '18 at 21:24
add a comment |
This regex could also work:
(?!(00000000))(d{8})
See Lookarounds (Usually) Want to be Anchored.
– Wiktor Stribiżew
Nov 13 '18 at 21:24
add a comment |
This regex could also work:
(?!(00000000))(d{8})
This regex could also work:
(?!(00000000))(d{8})
edited Nov 13 '18 at 20:18
Tiago Martins Peres
2,21341935
2,21341935
answered Nov 13 '18 at 20:11
ecaglecagl
1
1
See Lookarounds (Usually) Want to be Anchored.
– Wiktor Stribiżew
Nov 13 '18 at 21:24
add a comment |
See Lookarounds (Usually) Want to be Anchored.
– Wiktor Stribiżew
Nov 13 '18 at 21:24
See Lookarounds (Usually) Want to be Anchored.
– Wiktor Stribiżew
Nov 13 '18 at 21:24
See Lookarounds (Usually) Want to be Anchored.
– Wiktor Stribiżew
Nov 13 '18 at 21:24
add a 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%2f53287890%2fregex-to-search-through-a-file-for-a-set-of-parentheses-with-eight-characters-in%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
Yeah, use
grep -oP 'b(?=[01]{8}b)0*1[10]*'
– Wiktor Stribiżew
Nov 13 '18 at 19:10
See my answer below, I modified my comment as I did not realize you wanted to match these binary data inside parentheses.
– Wiktor Stribiżew
Nov 13 '18 at 19:34