Looking for a regular expression to match following
string = '***All Done***, ***Execution Success 5664 for $**$ files***'
re pattern = ([w+].*[$**]$.*[w+]|[w+])
output i'm getting: 'All Done***, ***Execution Success for $**$ files'
expected output: All Done Execution Success 5664 for $**$ files
Should get only alpha numeric characters and $**$
. Rest all should be cleared. Any help would be appreciated. In case if $**$
is not found on string, then it should return: All Done Execution Success 5664 for files
.
python regex
|
show 4 more comments
string = '***All Done***, ***Execution Success 5664 for $**$ files***'
re pattern = ([w+].*[$**]$.*[w+]|[w+])
output i'm getting: 'All Done***, ***Execution Success for $**$ files'
expected output: All Done Execution Success 5664 for $**$ files
Should get only alpha numeric characters and $**$
. Rest all should be cleared. Any help would be appreciated. In case if $**$
is not found on string, then it should return: All Done Execution Success 5664 for files
.
python regex
1
Tryre.sub(r'*3(.*?)*3|[^ws]', r'1', string)
(this is for Python 3.5+)
– Wiktor Stribiżew
Nov 15 '18 at 11:14
This did not work for me. String i've provided is just an example. It is not necessary to start with '***'. It can be like 'Error not ** found. Restore $**$ again **'. In this case i'm expecting regex to return 'Error not found Restore $**$ again'
– rubenhardy
Nov 15 '18 at 11:19
Then use ther'($**$)|[^ws]'
regex with the same code as above.
– Wiktor Stribiżew
Nov 15 '18 at 11:24
Tried this: 're.sub(r'($**$)|[^ws]', r'1', string)'. Got an exception: "raise error, "unmatched group" sre_constants.error: unmatched group"
– rubenhardy
Nov 15 '18 at 11:32
So, your Python is older than 3.5. Usere.sub(r'($**$)|[^ws]', lambda x: x.group(1) if x.group(1) else '', string)
– Wiktor Stribiżew
Nov 15 '18 at 11:33
|
show 4 more comments
string = '***All Done***, ***Execution Success 5664 for $**$ files***'
re pattern = ([w+].*[$**]$.*[w+]|[w+])
output i'm getting: 'All Done***, ***Execution Success for $**$ files'
expected output: All Done Execution Success 5664 for $**$ files
Should get only alpha numeric characters and $**$
. Rest all should be cleared. Any help would be appreciated. In case if $**$
is not found on string, then it should return: All Done Execution Success 5664 for files
.
python regex
string = '***All Done***, ***Execution Success 5664 for $**$ files***'
re pattern = ([w+].*[$**]$.*[w+]|[w+])
output i'm getting: 'All Done***, ***Execution Success for $**$ files'
expected output: All Done Execution Success 5664 for $**$ files
Should get only alpha numeric characters and $**$
. Rest all should be cleared. Any help would be appreciated. In case if $**$
is not found on string, then it should return: All Done Execution Success 5664 for files
.
python regex
python regex
edited Nov 15 '18 at 11:56
fewlinesofcode
2,1071819
2,1071819
asked Nov 15 '18 at 11:11
rubenhardyrubenhardy
92
92
1
Tryre.sub(r'*3(.*?)*3|[^ws]', r'1', string)
(this is for Python 3.5+)
– Wiktor Stribiżew
Nov 15 '18 at 11:14
This did not work for me. String i've provided is just an example. It is not necessary to start with '***'. It can be like 'Error not ** found. Restore $**$ again **'. In this case i'm expecting regex to return 'Error not found Restore $**$ again'
– rubenhardy
Nov 15 '18 at 11:19
Then use ther'($**$)|[^ws]'
regex with the same code as above.
– Wiktor Stribiżew
Nov 15 '18 at 11:24
Tried this: 're.sub(r'($**$)|[^ws]', r'1', string)'. Got an exception: "raise error, "unmatched group" sre_constants.error: unmatched group"
– rubenhardy
Nov 15 '18 at 11:32
So, your Python is older than 3.5. Usere.sub(r'($**$)|[^ws]', lambda x: x.group(1) if x.group(1) else '', string)
– Wiktor Stribiżew
Nov 15 '18 at 11:33
|
show 4 more comments
1
Tryre.sub(r'*3(.*?)*3|[^ws]', r'1', string)
(this is for Python 3.5+)
– Wiktor Stribiżew
Nov 15 '18 at 11:14
This did not work for me. String i've provided is just an example. It is not necessary to start with '***'. It can be like 'Error not ** found. Restore $**$ again **'. In this case i'm expecting regex to return 'Error not found Restore $**$ again'
– rubenhardy
Nov 15 '18 at 11:19
Then use ther'($**$)|[^ws]'
regex with the same code as above.
– Wiktor Stribiżew
Nov 15 '18 at 11:24
Tried this: 're.sub(r'($**$)|[^ws]', r'1', string)'. Got an exception: "raise error, "unmatched group" sre_constants.error: unmatched group"
– rubenhardy
Nov 15 '18 at 11:32
So, your Python is older than 3.5. Usere.sub(r'($**$)|[^ws]', lambda x: x.group(1) if x.group(1) else '', string)
– Wiktor Stribiżew
Nov 15 '18 at 11:33
1
1
Try
re.sub(r'*3(.*?)*3|[^ws]', r'1', string)
(this is for Python 3.5+)– Wiktor Stribiżew
Nov 15 '18 at 11:14
Try
re.sub(r'*3(.*?)*3|[^ws]', r'1', string)
(this is for Python 3.5+)– Wiktor Stribiżew
Nov 15 '18 at 11:14
This did not work for me. String i've provided is just an example. It is not necessary to start with '***'. It can be like 'Error not ** found. Restore $**$ again **'. In this case i'm expecting regex to return 'Error not found Restore $**$ again'
– rubenhardy
Nov 15 '18 at 11:19
This did not work for me. String i've provided is just an example. It is not necessary to start with '***'. It can be like 'Error not ** found. Restore $**$ again **'. In this case i'm expecting regex to return 'Error not found Restore $**$ again'
– rubenhardy
Nov 15 '18 at 11:19
Then use the
r'($**$)|[^ws]'
regex with the same code as above.– Wiktor Stribiżew
Nov 15 '18 at 11:24
Then use the
r'($**$)|[^ws]'
regex with the same code as above.– Wiktor Stribiżew
Nov 15 '18 at 11:24
Tried this: 're.sub(r'($**$)|[^ws]', r'1', string)'. Got an exception: "raise error, "unmatched group" sre_constants.error: unmatched group"
– rubenhardy
Nov 15 '18 at 11:32
Tried this: 're.sub(r'($**$)|[^ws]', r'1', string)'. Got an exception: "raise error, "unmatched group" sre_constants.error: unmatched group"
– rubenhardy
Nov 15 '18 at 11:32
So, your Python is older than 3.5. Use
re.sub(r'($**$)|[^ws]', lambda x: x.group(1) if x.group(1) else '', string)
– Wiktor Stribiżew
Nov 15 '18 at 11:33
So, your Python is older than 3.5. Use
re.sub(r'($**$)|[^ws]', lambda x: x.group(1) if x.group(1) else '', string)
– Wiktor Stribiżew
Nov 15 '18 at 11:33
|
show 4 more comments
1 Answer
1
active
oldest
votes
This seems to work for your provided examples (Python 3.6):
import re
strings = [
'***All Done***, ***Execution Success 5664 for $**$ files***',
'Error not ** found. Restore $**$ again **'
]
pattern = "w+|$**$"
cpattern = re.compile(pattern)
for string in strings:
print(" ".join(cpattern.findall(string)))
Output:
All Done Execution Success 5664 for $**$ files
Error not found Restore $**$ again
This worked excellently for my need. Thank you dabismal.
– rubenhardy
Nov 15 '18 at 11:43
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%2f53318184%2flooking-for-a-regular-expression-to-match-following%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
This seems to work for your provided examples (Python 3.6):
import re
strings = [
'***All Done***, ***Execution Success 5664 for $**$ files***',
'Error not ** found. Restore $**$ again **'
]
pattern = "w+|$**$"
cpattern = re.compile(pattern)
for string in strings:
print(" ".join(cpattern.findall(string)))
Output:
All Done Execution Success 5664 for $**$ files
Error not found Restore $**$ again
This worked excellently for my need. Thank you dabismal.
– rubenhardy
Nov 15 '18 at 11:43
add a comment |
This seems to work for your provided examples (Python 3.6):
import re
strings = [
'***All Done***, ***Execution Success 5664 for $**$ files***',
'Error not ** found. Restore $**$ again **'
]
pattern = "w+|$**$"
cpattern = re.compile(pattern)
for string in strings:
print(" ".join(cpattern.findall(string)))
Output:
All Done Execution Success 5664 for $**$ files
Error not found Restore $**$ again
This worked excellently for my need. Thank you dabismal.
– rubenhardy
Nov 15 '18 at 11:43
add a comment |
This seems to work for your provided examples (Python 3.6):
import re
strings = [
'***All Done***, ***Execution Success 5664 for $**$ files***',
'Error not ** found. Restore $**$ again **'
]
pattern = "w+|$**$"
cpattern = re.compile(pattern)
for string in strings:
print(" ".join(cpattern.findall(string)))
Output:
All Done Execution Success 5664 for $**$ files
Error not found Restore $**$ again
This seems to work for your provided examples (Python 3.6):
import re
strings = [
'***All Done***, ***Execution Success 5664 for $**$ files***',
'Error not ** found. Restore $**$ again **'
]
pattern = "w+|$**$"
cpattern = re.compile(pattern)
for string in strings:
print(" ".join(cpattern.findall(string)))
Output:
All Done Execution Success 5664 for $**$ files
Error not found Restore $**$ again
edited Nov 15 '18 at 11:41
answered Nov 15 '18 at 11:34
dabismaldabismal
12
12
This worked excellently for my need. Thank you dabismal.
– rubenhardy
Nov 15 '18 at 11:43
add a comment |
This worked excellently for my need. Thank you dabismal.
– rubenhardy
Nov 15 '18 at 11:43
This worked excellently for my need. Thank you dabismal.
– rubenhardy
Nov 15 '18 at 11:43
This worked excellently for my need. Thank you dabismal.
– rubenhardy
Nov 15 '18 at 11:43
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%2f53318184%2flooking-for-a-regular-expression-to-match-following%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
1
Try
re.sub(r'*3(.*?)*3|[^ws]', r'1', string)
(this is for Python 3.5+)– Wiktor Stribiżew
Nov 15 '18 at 11:14
This did not work for me. String i've provided is just an example. It is not necessary to start with '***'. It can be like 'Error not ** found. Restore $**$ again **'. In this case i'm expecting regex to return 'Error not found Restore $**$ again'
– rubenhardy
Nov 15 '18 at 11:19
Then use the
r'($**$)|[^ws]'
regex with the same code as above.– Wiktor Stribiżew
Nov 15 '18 at 11:24
Tried this: 're.sub(r'($**$)|[^ws]', r'1', string)'. Got an exception: "raise error, "unmatched group" sre_constants.error: unmatched group"
– rubenhardy
Nov 15 '18 at 11:32
So, your Python is older than 3.5. Use
re.sub(r'($**$)|[^ws]', lambda x: x.group(1) if x.group(1) else '', string)
– Wiktor Stribiżew
Nov 15 '18 at 11:33