re.sub how to do it in one step?












1















I try to put this steps in one, but it doesnt work



w = re.sub('[0-9]', r'9', w)
w = re.sub('[A-Z]', r'X', w)
w = re.sub('[a-z]', r'x', w)


Does anybody knows how to make from such strings as XXxxxx999 --> Xx9.










share|improve this question




















  • 2





    Настя, you may use a callback method as the replacement argument. There, you may apply custom replacement logic, just build your regex with capturing groups. like ([0-9])|([A-Z])|[a-z], and then analyze which group matched.

    – Wiktor Stribiżew
    Nov 13 '18 at 13:33











  • I understood it would be like that, but i dont understand how to make in replacement some options w = re.sub('([0-9])|([A-Z])|([a-z])', r'9', w)

    – Nastja Kr
    Nov 13 '18 at 13:38








  • 1





    re.sub(r'([0-9])|([A-Z])|[a-z]', repl, w) where repl is def repl(m):...., just check if m.group(1) then it is a digit, if m.group(2) then it is an uppercase letter...

    – Wiktor Stribiżew
    Nov 13 '18 at 13:46


















1















I try to put this steps in one, but it doesnt work



w = re.sub('[0-9]', r'9', w)
w = re.sub('[A-Z]', r'X', w)
w = re.sub('[a-z]', r'x', w)


Does anybody knows how to make from such strings as XXxxxx999 --> Xx9.










share|improve this question




















  • 2





    Настя, you may use a callback method as the replacement argument. There, you may apply custom replacement logic, just build your regex with capturing groups. like ([0-9])|([A-Z])|[a-z], and then analyze which group matched.

    – Wiktor Stribiżew
    Nov 13 '18 at 13:33











  • I understood it would be like that, but i dont understand how to make in replacement some options w = re.sub('([0-9])|([A-Z])|([a-z])', r'9', w)

    – Nastja Kr
    Nov 13 '18 at 13:38








  • 1





    re.sub(r'([0-9])|([A-Z])|[a-z]', repl, w) where repl is def repl(m):...., just check if m.group(1) then it is a digit, if m.group(2) then it is an uppercase letter...

    – Wiktor Stribiżew
    Nov 13 '18 at 13:46
















1












1








1








I try to put this steps in one, but it doesnt work



w = re.sub('[0-9]', r'9', w)
w = re.sub('[A-Z]', r'X', w)
w = re.sub('[a-z]', r'x', w)


Does anybody knows how to make from such strings as XXxxxx999 --> Xx9.










share|improve this question
















I try to put this steps in one, but it doesnt work



w = re.sub('[0-9]', r'9', w)
w = re.sub('[A-Z]', r'X', w)
w = re.sub('[a-z]', r'x', w)


Does anybody knows how to make from such strings as XXxxxx999 --> Xx9.







regex python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 14:56









Wiktor Stribiżew

312k16132207




312k16132207










asked Nov 13 '18 at 13:25









Nastja KrNastja Kr

687




687








  • 2





    Настя, you may use a callback method as the replacement argument. There, you may apply custom replacement logic, just build your regex with capturing groups. like ([0-9])|([A-Z])|[a-z], and then analyze which group matched.

    – Wiktor Stribiżew
    Nov 13 '18 at 13:33











  • I understood it would be like that, but i dont understand how to make in replacement some options w = re.sub('([0-9])|([A-Z])|([a-z])', r'9', w)

    – Nastja Kr
    Nov 13 '18 at 13:38








  • 1





    re.sub(r'([0-9])|([A-Z])|[a-z]', repl, w) where repl is def repl(m):...., just check if m.group(1) then it is a digit, if m.group(2) then it is an uppercase letter...

    – Wiktor Stribiżew
    Nov 13 '18 at 13:46
















  • 2





    Настя, you may use a callback method as the replacement argument. There, you may apply custom replacement logic, just build your regex with capturing groups. like ([0-9])|([A-Z])|[a-z], and then analyze which group matched.

    – Wiktor Stribiżew
    Nov 13 '18 at 13:33











  • I understood it would be like that, but i dont understand how to make in replacement some options w = re.sub('([0-9])|([A-Z])|([a-z])', r'9', w)

    – Nastja Kr
    Nov 13 '18 at 13:38








  • 1





    re.sub(r'([0-9])|([A-Z])|[a-z]', repl, w) where repl is def repl(m):...., just check if m.group(1) then it is a digit, if m.group(2) then it is an uppercase letter...

    – Wiktor Stribiżew
    Nov 13 '18 at 13:46










2




2





Настя, you may use a callback method as the replacement argument. There, you may apply custom replacement logic, just build your regex with capturing groups. like ([0-9])|([A-Z])|[a-z], and then analyze which group matched.

– Wiktor Stribiżew
Nov 13 '18 at 13:33





Настя, you may use a callback method as the replacement argument. There, you may apply custom replacement logic, just build your regex with capturing groups. like ([0-9])|([A-Z])|[a-z], and then analyze which group matched.

– Wiktor Stribiżew
Nov 13 '18 at 13:33













I understood it would be like that, but i dont understand how to make in replacement some options w = re.sub('([0-9])|([A-Z])|([a-z])', r'9', w)

– Nastja Kr
Nov 13 '18 at 13:38







I understood it would be like that, but i dont understand how to make in replacement some options w = re.sub('([0-9])|([A-Z])|([a-z])', r'9', w)

– Nastja Kr
Nov 13 '18 at 13:38






1




1





re.sub(r'([0-9])|([A-Z])|[a-z]', repl, w) where repl is def repl(m):...., just check if m.group(1) then it is a digit, if m.group(2) then it is an uppercase letter...

– Wiktor Stribiżew
Nov 13 '18 at 13:46







re.sub(r'([0-9])|([A-Z])|[a-z]', repl, w) where repl is def repl(m):...., just check if m.group(1) then it is a digit, if m.group(2) then it is an uppercase letter...

– Wiktor Stribiżew
Nov 13 '18 at 13:46














1 Answer
1






active

oldest

votes


















2














You may use a callback method as a replacement argument like this:



import re

rx = r'([0-9]+)|([A-Z]+)|[a-z]+'
w = "XXxxxx999"

def repl(m):
if m.group(1): # if ([0-9]) matched
return '9' # replace with 9
elif m.group(2): # if ([A-Z]) matched
return 'X' # replace with X
else: # if ([a-z]) matched
return 'x' # replace with x

print(re.sub(rx, repl, w)) # => Xx9


See the Python demo.



The regex matches:





  • ([0-9]+) - Group 1: 1+ digits


  • | - or


  • ([A-Z]+) - Group 2: 1+ uppercase letters


  • | - or


  • [a-z]+ - 1+ lowercase letters.






share|improve this answer


























  • great, thanks I am new in programming so I have learned today somthing. Can you laso help me with the second question ?I found some solutions for this problem, but only if we want to eliminate all the characters, that appear in the string, and not like in my case. input = "XXXxxx999XX" output = "Xx9X"

    – Nastja Kr
    Nov 13 '18 at 14:40






  • 1





    @NastjaKryvoscheya Do you mean ideone.com/iPPeVF ? Use rx = r'([0-9]+)|([A-Z]+)|[a-z]+' where + quantifier is added to match 1 or more occurrences of each subpattern.

    – Wiktor Stribiżew
    Nov 13 '18 at 14:47













  • wow great, thanks.... didnt thought it is so easy

    – Nastja Kr
    Nov 13 '18 at 14:53











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%2f53282036%2fre-sub-how-to-do-it-in-one-step%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









2














You may use a callback method as a replacement argument like this:



import re

rx = r'([0-9]+)|([A-Z]+)|[a-z]+'
w = "XXxxxx999"

def repl(m):
if m.group(1): # if ([0-9]) matched
return '9' # replace with 9
elif m.group(2): # if ([A-Z]) matched
return 'X' # replace with X
else: # if ([a-z]) matched
return 'x' # replace with x

print(re.sub(rx, repl, w)) # => Xx9


See the Python demo.



The regex matches:





  • ([0-9]+) - Group 1: 1+ digits


  • | - or


  • ([A-Z]+) - Group 2: 1+ uppercase letters


  • | - or


  • [a-z]+ - 1+ lowercase letters.






share|improve this answer


























  • great, thanks I am new in programming so I have learned today somthing. Can you laso help me with the second question ?I found some solutions for this problem, but only if we want to eliminate all the characters, that appear in the string, and not like in my case. input = "XXXxxx999XX" output = "Xx9X"

    – Nastja Kr
    Nov 13 '18 at 14:40






  • 1





    @NastjaKryvoscheya Do you mean ideone.com/iPPeVF ? Use rx = r'([0-9]+)|([A-Z]+)|[a-z]+' where + quantifier is added to match 1 or more occurrences of each subpattern.

    – Wiktor Stribiżew
    Nov 13 '18 at 14:47













  • wow great, thanks.... didnt thought it is so easy

    – Nastja Kr
    Nov 13 '18 at 14:53
















2














You may use a callback method as a replacement argument like this:



import re

rx = r'([0-9]+)|([A-Z]+)|[a-z]+'
w = "XXxxxx999"

def repl(m):
if m.group(1): # if ([0-9]) matched
return '9' # replace with 9
elif m.group(2): # if ([A-Z]) matched
return 'X' # replace with X
else: # if ([a-z]) matched
return 'x' # replace with x

print(re.sub(rx, repl, w)) # => Xx9


See the Python demo.



The regex matches:





  • ([0-9]+) - Group 1: 1+ digits


  • | - or


  • ([A-Z]+) - Group 2: 1+ uppercase letters


  • | - or


  • [a-z]+ - 1+ lowercase letters.






share|improve this answer


























  • great, thanks I am new in programming so I have learned today somthing. Can you laso help me with the second question ?I found some solutions for this problem, but only if we want to eliminate all the characters, that appear in the string, and not like in my case. input = "XXXxxx999XX" output = "Xx9X"

    – Nastja Kr
    Nov 13 '18 at 14:40






  • 1





    @NastjaKryvoscheya Do you mean ideone.com/iPPeVF ? Use rx = r'([0-9]+)|([A-Z]+)|[a-z]+' where + quantifier is added to match 1 or more occurrences of each subpattern.

    – Wiktor Stribiżew
    Nov 13 '18 at 14:47













  • wow great, thanks.... didnt thought it is so easy

    – Nastja Kr
    Nov 13 '18 at 14:53














2












2








2







You may use a callback method as a replacement argument like this:



import re

rx = r'([0-9]+)|([A-Z]+)|[a-z]+'
w = "XXxxxx999"

def repl(m):
if m.group(1): # if ([0-9]) matched
return '9' # replace with 9
elif m.group(2): # if ([A-Z]) matched
return 'X' # replace with X
else: # if ([a-z]) matched
return 'x' # replace with x

print(re.sub(rx, repl, w)) # => Xx9


See the Python demo.



The regex matches:





  • ([0-9]+) - Group 1: 1+ digits


  • | - or


  • ([A-Z]+) - Group 2: 1+ uppercase letters


  • | - or


  • [a-z]+ - 1+ lowercase letters.






share|improve this answer















You may use a callback method as a replacement argument like this:



import re

rx = r'([0-9]+)|([A-Z]+)|[a-z]+'
w = "XXxxxx999"

def repl(m):
if m.group(1): # if ([0-9]) matched
return '9' # replace with 9
elif m.group(2): # if ([A-Z]) matched
return 'X' # replace with X
else: # if ([a-z]) matched
return 'x' # replace with x

print(re.sub(rx, repl, w)) # => Xx9


See the Python demo.



The regex matches:





  • ([0-9]+) - Group 1: 1+ digits


  • | - or


  • ([A-Z]+) - Group 2: 1+ uppercase letters


  • | - or


  • [a-z]+ - 1+ lowercase letters.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 14:57

























answered Nov 13 '18 at 14:17









Wiktor StribiżewWiktor Stribiżew

312k16132207




312k16132207













  • great, thanks I am new in programming so I have learned today somthing. Can you laso help me with the second question ?I found some solutions for this problem, but only if we want to eliminate all the characters, that appear in the string, and not like in my case. input = "XXXxxx999XX" output = "Xx9X"

    – Nastja Kr
    Nov 13 '18 at 14:40






  • 1





    @NastjaKryvoscheya Do you mean ideone.com/iPPeVF ? Use rx = r'([0-9]+)|([A-Z]+)|[a-z]+' where + quantifier is added to match 1 or more occurrences of each subpattern.

    – Wiktor Stribiżew
    Nov 13 '18 at 14:47













  • wow great, thanks.... didnt thought it is so easy

    – Nastja Kr
    Nov 13 '18 at 14:53



















  • great, thanks I am new in programming so I have learned today somthing. Can you laso help me with the second question ?I found some solutions for this problem, but only if we want to eliminate all the characters, that appear in the string, and not like in my case. input = "XXXxxx999XX" output = "Xx9X"

    – Nastja Kr
    Nov 13 '18 at 14:40






  • 1





    @NastjaKryvoscheya Do you mean ideone.com/iPPeVF ? Use rx = r'([0-9]+)|([A-Z]+)|[a-z]+' where + quantifier is added to match 1 or more occurrences of each subpattern.

    – Wiktor Stribiżew
    Nov 13 '18 at 14:47













  • wow great, thanks.... didnt thought it is so easy

    – Nastja Kr
    Nov 13 '18 at 14:53

















great, thanks I am new in programming so I have learned today somthing. Can you laso help me with the second question ?I found some solutions for this problem, but only if we want to eliminate all the characters, that appear in the string, and not like in my case. input = "XXXxxx999XX" output = "Xx9X"

– Nastja Kr
Nov 13 '18 at 14:40





great, thanks I am new in programming so I have learned today somthing. Can you laso help me with the second question ?I found some solutions for this problem, but only if we want to eliminate all the characters, that appear in the string, and not like in my case. input = "XXXxxx999XX" output = "Xx9X"

– Nastja Kr
Nov 13 '18 at 14:40




1




1





@NastjaKryvoscheya Do you mean ideone.com/iPPeVF ? Use rx = r'([0-9]+)|([A-Z]+)|[a-z]+' where + quantifier is added to match 1 or more occurrences of each subpattern.

– Wiktor Stribiżew
Nov 13 '18 at 14:47







@NastjaKryvoscheya Do you mean ideone.com/iPPeVF ? Use rx = r'([0-9]+)|([A-Z]+)|[a-z]+' where + quantifier is added to match 1 or more occurrences of each subpattern.

– Wiktor Stribiżew
Nov 13 '18 at 14:47















wow great, thanks.... didnt thought it is so easy

– Nastja Kr
Nov 13 '18 at 14:53





wow great, thanks.... didnt thought it is so easy

– Nastja Kr
Nov 13 '18 at 14:53


















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%2f53282036%2fre-sub-how-to-do-it-in-one-step%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







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma