Regex: Match/Delete everything on a line except parenthesis (keep the parentheses and delete the rest of the...
I have this kind of line:
Word (mother) word 33 (453) word word 444 (4) word
The result should be: (mother) (453) (4)
I want to keep the parentheses and delete the rest of the words on the line. I try this regex, but not too good :(
Search: ([^!(]*?)|(|)
Replace by: 1
windows-10 notepad++ regex
add a comment |
I have this kind of line:
Word (mother) word 33 (453) word word 444 (4) word
The result should be: (mother) (453) (4)
I want to keep the parentheses and delete the rest of the words on the line. I try this regex, but not too good :(
Search: ([^!(]*?)|(|)
Replace by: 1
windows-10 notepad++ regex
Please post the result you want to achieve for the example above.
– harrymc
Feb 9 at 11:16
add a comment |
I have this kind of line:
Word (mother) word 33 (453) word word 444 (4) word
The result should be: (mother) (453) (4)
I want to keep the parentheses and delete the rest of the words on the line. I try this regex, but not too good :(
Search: ([^!(]*?)|(|)
Replace by: 1
windows-10 notepad++ regex
I have this kind of line:
Word (mother) word 33 (453) word word 444 (4) word
The result should be: (mother) (453) (4)
I want to keep the parentheses and delete the rest of the words on the line. I try this regex, but not too good :(
Search: ([^!(]*?)|(|)
Replace by: 1
windows-10 notepad++ regex
windows-10 notepad++ regex
edited Feb 9 at 11:19
Just Me
asked Feb 9 at 11:12
Just MeJust Me
1819
1819
Please post the result you want to achieve for the example above.
– harrymc
Feb 9 at 11:16
add a comment |
Please post the result you want to achieve for the example above.
– harrymc
Feb 9 at 11:16
Please post the result you want to achieve for the example above.
– harrymc
Feb 9 at 11:16
Please post the result you want to achieve for the example above.
– harrymc
Feb 9 at 11:16
add a comment |
3 Answers
3
active
oldest
votes
Ctrl+H
- Find what:
(?:^|G)(?:h*w+h*)+((w+)h*)|(?:h*w+)*$
- Replace with:
$1
- check Wrap around
- check Regular expression
- Replace all
Explanation:
(?:^|G) # non capture group, beginning of line OR restart from last match position
(?: # non capture group
h* # 0 or more horizontal spaces
w+ # 1 or more word characters
h* # 0 or more horizontal spaces
)+ # end group, may appear 1 or more times
( # start group 1
(w+) # 1 or more word characters surounded by parenthesis
h* # 0 or more horizontal spaces
) # end group 1
| # OR
(?: # non capture group
h*w+ # 0 or more horizontal spaces, followed by 1 or more word characters
)* # group may appear 0 or more times
$ # end of line
Result for given example:
(mother) (453) (4)
add a comment |
first replace
edited:
.*?((.*?))
with
1
then, replace
^(.*)).*
with
1
Final output
(mother)(453)(4)
1
What's the meaning of the first quotation mark? Did you really try it?
– Toto
Feb 9 at 12:47
You are right. That was wrong regex. As I had done it in two parts, the first regex was copied to clipboard but got mixed up when I pasted here from clipboard. Now edited to post the correct one. I am sorry for the mistake. I appreciate your highlighting the error in time.
– VSRawat
Feb 9 at 13:03
add a comment |
A simple regex to extract a word with its parenthesis is:
(([^)]+))
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
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%2fsuperuser.com%2fquestions%2f1403824%2fregex-match-delete-everything-on-a-line-except-parenthesis-keep-the-parenthese%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
Ctrl+H
- Find what:
(?:^|G)(?:h*w+h*)+((w+)h*)|(?:h*w+)*$
- Replace with:
$1
- check Wrap around
- check Regular expression
- Replace all
Explanation:
(?:^|G) # non capture group, beginning of line OR restart from last match position
(?: # non capture group
h* # 0 or more horizontal spaces
w+ # 1 or more word characters
h* # 0 or more horizontal spaces
)+ # end group, may appear 1 or more times
( # start group 1
(w+) # 1 or more word characters surounded by parenthesis
h* # 0 or more horizontal spaces
) # end group 1
| # OR
(?: # non capture group
h*w+ # 0 or more horizontal spaces, followed by 1 or more word characters
)* # group may appear 0 or more times
$ # end of line
Result for given example:
(mother) (453) (4)
add a comment |
Ctrl+H
- Find what:
(?:^|G)(?:h*w+h*)+((w+)h*)|(?:h*w+)*$
- Replace with:
$1
- check Wrap around
- check Regular expression
- Replace all
Explanation:
(?:^|G) # non capture group, beginning of line OR restart from last match position
(?: # non capture group
h* # 0 or more horizontal spaces
w+ # 1 or more word characters
h* # 0 or more horizontal spaces
)+ # end group, may appear 1 or more times
( # start group 1
(w+) # 1 or more word characters surounded by parenthesis
h* # 0 or more horizontal spaces
) # end group 1
| # OR
(?: # non capture group
h*w+ # 0 or more horizontal spaces, followed by 1 or more word characters
)* # group may appear 0 or more times
$ # end of line
Result for given example:
(mother) (453) (4)
add a comment |
Ctrl+H
- Find what:
(?:^|G)(?:h*w+h*)+((w+)h*)|(?:h*w+)*$
- Replace with:
$1
- check Wrap around
- check Regular expression
- Replace all
Explanation:
(?:^|G) # non capture group, beginning of line OR restart from last match position
(?: # non capture group
h* # 0 or more horizontal spaces
w+ # 1 or more word characters
h* # 0 or more horizontal spaces
)+ # end group, may appear 1 or more times
( # start group 1
(w+) # 1 or more word characters surounded by parenthesis
h* # 0 or more horizontal spaces
) # end group 1
| # OR
(?: # non capture group
h*w+ # 0 or more horizontal spaces, followed by 1 or more word characters
)* # group may appear 0 or more times
$ # end of line
Result for given example:
(mother) (453) (4)
Ctrl+H
- Find what:
(?:^|G)(?:h*w+h*)+((w+)h*)|(?:h*w+)*$
- Replace with:
$1
- check Wrap around
- check Regular expression
- Replace all
Explanation:
(?:^|G) # non capture group, beginning of line OR restart from last match position
(?: # non capture group
h* # 0 or more horizontal spaces
w+ # 1 or more word characters
h* # 0 or more horizontal spaces
)+ # end group, may appear 1 or more times
( # start group 1
(w+) # 1 or more word characters surounded by parenthesis
h* # 0 or more horizontal spaces
) # end group 1
| # OR
(?: # non capture group
h*w+ # 0 or more horizontal spaces, followed by 1 or more word characters
)* # group may appear 0 or more times
$ # end of line
Result for given example:
(mother) (453) (4)
answered Feb 9 at 11:30
TotoToto
3,914101226
3,914101226
add a comment |
add a comment |
first replace
edited:
.*?((.*?))
with
1
then, replace
^(.*)).*
with
1
Final output
(mother)(453)(4)
1
What's the meaning of the first quotation mark? Did you really try it?
– Toto
Feb 9 at 12:47
You are right. That was wrong regex. As I had done it in two parts, the first regex was copied to clipboard but got mixed up when I pasted here from clipboard. Now edited to post the correct one. I am sorry for the mistake. I appreciate your highlighting the error in time.
– VSRawat
Feb 9 at 13:03
add a comment |
first replace
edited:
.*?((.*?))
with
1
then, replace
^(.*)).*
with
1
Final output
(mother)(453)(4)
1
What's the meaning of the first quotation mark? Did you really try it?
– Toto
Feb 9 at 12:47
You are right. That was wrong regex. As I had done it in two parts, the first regex was copied to clipboard but got mixed up when I pasted here from clipboard. Now edited to post the correct one. I am sorry for the mistake. I appreciate your highlighting the error in time.
– VSRawat
Feb 9 at 13:03
add a comment |
first replace
edited:
.*?((.*?))
with
1
then, replace
^(.*)).*
with
1
Final output
(mother)(453)(4)
first replace
edited:
.*?((.*?))
with
1
then, replace
^(.*)).*
with
1
Final output
(mother)(453)(4)
edited Feb 9 at 13:00
answered Feb 9 at 12:40
VSRawatVSRawat
15512
15512
1
What's the meaning of the first quotation mark? Did you really try it?
– Toto
Feb 9 at 12:47
You are right. That was wrong regex. As I had done it in two parts, the first regex was copied to clipboard but got mixed up when I pasted here from clipboard. Now edited to post the correct one. I am sorry for the mistake. I appreciate your highlighting the error in time.
– VSRawat
Feb 9 at 13:03
add a comment |
1
What's the meaning of the first quotation mark? Did you really try it?
– Toto
Feb 9 at 12:47
You are right. That was wrong regex. As I had done it in two parts, the first regex was copied to clipboard but got mixed up when I pasted here from clipboard. Now edited to post the correct one. I am sorry for the mistake. I appreciate your highlighting the error in time.
– VSRawat
Feb 9 at 13:03
1
1
What's the meaning of the first quotation mark? Did you really try it?
– Toto
Feb 9 at 12:47
What's the meaning of the first quotation mark? Did you really try it?
– Toto
Feb 9 at 12:47
You are right. That was wrong regex. As I had done it in two parts, the first regex was copied to clipboard but got mixed up when I pasted here from clipboard. Now edited to post the correct one. I am sorry for the mistake. I appreciate your highlighting the error in time.
– VSRawat
Feb 9 at 13:03
You are right. That was wrong regex. As I had done it in two parts, the first regex was copied to clipboard but got mixed up when I pasted here from clipboard. Now edited to post the correct one. I am sorry for the mistake. I appreciate your highlighting the error in time.
– VSRawat
Feb 9 at 13:03
add a comment |
A simple regex to extract a word with its parenthesis is:
(([^)]+))
add a comment |
A simple regex to extract a word with its parenthesis is:
(([^)]+))
add a comment |
A simple regex to extract a word with its parenthesis is:
(([^)]+))
A simple regex to extract a word with its parenthesis is:
(([^)]+))
answered Feb 9 at 12:20
harrymcharrymc
262k14271578
262k14271578
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- 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%2fsuperuser.com%2fquestions%2f1403824%2fregex-match-delete-everything-on-a-line-except-parenthesis-keep-the-parenthese%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
Please post the result you want to achieve for the example above.
– harrymc
Feb 9 at 11:16