Looking for a regular expression to match following










0















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.










share|improve this question



















  • 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
















0















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.










share|improve this question



















  • 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














0












0








0








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 11:56









fewlinesofcode

2,1071819




2,1071819










asked Nov 15 '18 at 11:11









rubenhardyrubenhardy

92




92







  • 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













  • 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








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













1 Answer
1






active

oldest

votes


















0














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






share|improve this answer

























  • This worked excellently for my need. Thank you dabismal.

    – rubenhardy
    Nov 15 '18 at 11:43










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
);



);













draft saved

draft discarded


















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









0














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






share|improve this answer

























  • This worked excellently for my need. Thank you dabismal.

    – rubenhardy
    Nov 15 '18 at 11:43















0














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






share|improve this answer

























  • This worked excellently for my need. Thank you dabismal.

    – rubenhardy
    Nov 15 '18 at 11:43













0












0








0







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






share|improve this answer















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







share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3