Regex version of strip() - Ch. 7 Automate the Boring StuffRemoving Asterisks and neighbors from a...
Should I include fillets on my 3d printed parts?
Are there examples of rowers who also fought?
Why "amatus est" instead of "*amavitur"
Is swap gate equivalent to just exchanging the wire of the two qubits?
What does "vrit' mean with reference to documentaries?
How to recover a single blank shot from a film camera
Is a sequel allowed to start before the end of the first book?
Is this a valid proof that A = B given A ∩ B = A ∪ B?
How could I create a situation in which a PC has to make a saving throw or be forced to pet a dog?
how to find which software is doing ssh connection?
How to ask if I can mow my neighbor's lawn
Why was New Asgard established at this place?
What is the word?
How can the US president give an order to a civilian?
Co-worker is now managing my team. Does this mean that I'm being demoted?
Is it a bad idea to have a pen name with only an initial for a surname?
I just entered the USA without passport control at Atlanta airport
How can caller ID be faked?
In the US, can a former president run again?
Why do you need to heat the pan before heating the olive oil?
How to make all magic-casting innate, but still rare?
Weird thing in 737 cabin
Fibonacci sequence and other metallic sequences emerged in the form of fractions
First occurrence in the Sixers sequence
Regex version of strip() - Ch. 7 Automate the Boring Stuff
Removing Asterisks and neighbors from a stringCommand TokenizerComma Code - Automate the Boring StuffPython Automate the Boring Stuff Collatz exerciseStrong Password DetectionStrong password checker in PythonComma Code - Ch. 4 Automate the Boring StuffAutomate the Boring Stuff - Collatz ExerciseCharacter Picture Grid exercise - automatetheboringstuffFantasy game inventory — Ch. 5 Automate the Boring Stuff
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
$begingroup$
Here is a practice exercise — Regex version of strip()
$-$
Write a function that takes a string and does the same thing as the
strip()
string method. If no other arguments are passed other than the
string to strip, then whitespace characters will be removed from the
beginning and end of the string. Otherwise, the characters specified
in the second argument to the function will be removed from the
string.
I have written the following code. Is there any better way to write it? Any feedback is highly appreciated.
import re
def regex_strip(s, chars = None):
if chars == None:
strip_left = re.compile(r'^s*')
strip_right = re.compile(r's*$')
s = re.sub(strip_left, "", s)
s = re.sub(strip_right, "", s)
else:
strip_left = re.compile(r'^[' + re.escape(chars) + r']*')
strip_right = re.compile(r'[' + re.escape(chars) + r']*$')
s = re.sub(strip_left, "", s)
s = re.sub(strip_right, "", s)
return s
Here is an example output -
s = '.* alphabetatheta *4453 +-'
print(regex_strip(s, '.+-*'))
>>> alphabetatheta *4453
python performance python-3.x regex reinventing-the-wheel
$endgroup$
add a comment |
$begingroup$
Here is a practice exercise — Regex version of strip()
$-$
Write a function that takes a string and does the same thing as the
strip()
string method. If no other arguments are passed other than the
string to strip, then whitespace characters will be removed from the
beginning and end of the string. Otherwise, the characters specified
in the second argument to the function will be removed from the
string.
I have written the following code. Is there any better way to write it? Any feedback is highly appreciated.
import re
def regex_strip(s, chars = None):
if chars == None:
strip_left = re.compile(r'^s*')
strip_right = re.compile(r's*$')
s = re.sub(strip_left, "", s)
s = re.sub(strip_right, "", s)
else:
strip_left = re.compile(r'^[' + re.escape(chars) + r']*')
strip_right = re.compile(r'[' + re.escape(chars) + r']*$')
s = re.sub(strip_left, "", s)
s = re.sub(strip_right, "", s)
return s
Here is an example output -
s = '.* alphabetatheta *4453 +-'
print(regex_strip(s, '.+-*'))
>>> alphabetatheta *4453
python performance python-3.x regex reinventing-the-wheel
$endgroup$
$begingroup$
It all depends whether s includes all the white space characters. There are tons of them: en.wikipedia.org/wiki/Whitespace_character
$endgroup$
– dfhwze
9 hours ago
add a comment |
$begingroup$
Here is a practice exercise — Regex version of strip()
$-$
Write a function that takes a string and does the same thing as the
strip()
string method. If no other arguments are passed other than the
string to strip, then whitespace characters will be removed from the
beginning and end of the string. Otherwise, the characters specified
in the second argument to the function will be removed from the
string.
I have written the following code. Is there any better way to write it? Any feedback is highly appreciated.
import re
def regex_strip(s, chars = None):
if chars == None:
strip_left = re.compile(r'^s*')
strip_right = re.compile(r's*$')
s = re.sub(strip_left, "", s)
s = re.sub(strip_right, "", s)
else:
strip_left = re.compile(r'^[' + re.escape(chars) + r']*')
strip_right = re.compile(r'[' + re.escape(chars) + r']*$')
s = re.sub(strip_left, "", s)
s = re.sub(strip_right, "", s)
return s
Here is an example output -
s = '.* alphabetatheta *4453 +-'
print(regex_strip(s, '.+-*'))
>>> alphabetatheta *4453
python performance python-3.x regex reinventing-the-wheel
$endgroup$
Here is a practice exercise — Regex version of strip()
$-$
Write a function that takes a string and does the same thing as the
strip()
string method. If no other arguments are passed other than the
string to strip, then whitespace characters will be removed from the
beginning and end of the string. Otherwise, the characters specified
in the second argument to the function will be removed from the
string.
I have written the following code. Is there any better way to write it? Any feedback is highly appreciated.
import re
def regex_strip(s, chars = None):
if chars == None:
strip_left = re.compile(r'^s*')
strip_right = re.compile(r's*$')
s = re.sub(strip_left, "", s)
s = re.sub(strip_right, "", s)
else:
strip_left = re.compile(r'^[' + re.escape(chars) + r']*')
strip_right = re.compile(r'[' + re.escape(chars) + r']*$')
s = re.sub(strip_left, "", s)
s = re.sub(strip_right, "", s)
return s
Here is an example output -
s = '.* alphabetatheta *4453 +-'
print(regex_strip(s, '.+-*'))
>>> alphabetatheta *4453
python performance python-3.x regex reinventing-the-wheel
python performance python-3.x regex reinventing-the-wheel
edited 7 hours ago
200_success
134k21166439
134k21166439
asked 9 hours ago
JustinJustin
1,675528
1,675528
$begingroup$
It all depends whether s includes all the white space characters. There are tons of them: en.wikipedia.org/wiki/Whitespace_character
$endgroup$
– dfhwze
9 hours ago
add a comment |
$begingroup$
It all depends whether s includes all the white space characters. There are tons of them: en.wikipedia.org/wiki/Whitespace_character
$endgroup$
– dfhwze
9 hours ago
$begingroup$
It all depends whether s includes all the white space characters. There are tons of them: en.wikipedia.org/wiki/Whitespace_character
$endgroup$
– dfhwze
9 hours ago
$begingroup$
It all depends whether s includes all the white space characters. There are tons of them: en.wikipedia.org/wiki/Whitespace_character
$endgroup$
– dfhwze
9 hours ago
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
If you call regex_strip(s, "")
, you will get:
re.error: unterminated character set at position 0
because neither ^[]
nor []$
is a value regular expression. You could avoid this by using if not chars:
instead of if chars == None:
.
There is no need to re.compile()
your regular expressions; you aren't saving the compiled patterns anywhere for re-use.
You can simplify your logic by using the reg-ex to capture the middle, non-stripped portion of the string, instead of doing two replacements for the start and end trim operations:
import re
def regex_strip(s, chars = None):
if chars:
trim = '[' + re.escape(chars) + ']*'
else:
trim = r's*'
return re.fullmatch(f"{trim}(.*?){trim}", s).group(1)
I'm not sure the point of asking you to write your own strip()
function is to delegate the task to the reg-ex engine. It seems like going out and buying a sledge hammer when the problem is to build a nut cracker.
$endgroup$
add a comment |
$begingroup$
DRY. Both branches do identical re.sub
s. Take them out:
if chars == None:
strip_left = re.compile(r'^s*')
strip_right = re.compile(r's*$')
else:
strip_left = re.compile(r'^[' + re.escape(chars) + r']*')
strip_right = re.compile(r'[' + re.escape(chars) + r']*$')
s = re.sub(strip_left, "", s)
s = re.sub(strip_right, "", s)
return s
I recommend to go one step further, and unify the computation of strip_*
:
if chars == None:
chars = string.whitespace
strip_left = re.compile(r'^[' + re.escape(chars) + r']*')
strip_right = re.compile(r'[' + re.escape(chars) + r']*$')
s = re.sub(strip_left, "", s)
s = re.sub(strip_right, "", s)
return s
It is recommended to compare against None
as chars is None
rather than using ==
.
$endgroup$
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: "196"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fcodereview.stackexchange.com%2fquestions%2f222372%2fregex-version-of-strip-ch-7-automate-the-boring-stuff%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
If you call regex_strip(s, "")
, you will get:
re.error: unterminated character set at position 0
because neither ^[]
nor []$
is a value regular expression. You could avoid this by using if not chars:
instead of if chars == None:
.
There is no need to re.compile()
your regular expressions; you aren't saving the compiled patterns anywhere for re-use.
You can simplify your logic by using the reg-ex to capture the middle, non-stripped portion of the string, instead of doing two replacements for the start and end trim operations:
import re
def regex_strip(s, chars = None):
if chars:
trim = '[' + re.escape(chars) + ']*'
else:
trim = r's*'
return re.fullmatch(f"{trim}(.*?){trim}", s).group(1)
I'm not sure the point of asking you to write your own strip()
function is to delegate the task to the reg-ex engine. It seems like going out and buying a sledge hammer when the problem is to build a nut cracker.
$endgroup$
add a comment |
$begingroup$
If you call regex_strip(s, "")
, you will get:
re.error: unterminated character set at position 0
because neither ^[]
nor []$
is a value regular expression. You could avoid this by using if not chars:
instead of if chars == None:
.
There is no need to re.compile()
your regular expressions; you aren't saving the compiled patterns anywhere for re-use.
You can simplify your logic by using the reg-ex to capture the middle, non-stripped portion of the string, instead of doing two replacements for the start and end trim operations:
import re
def regex_strip(s, chars = None):
if chars:
trim = '[' + re.escape(chars) + ']*'
else:
trim = r's*'
return re.fullmatch(f"{trim}(.*?){trim}", s).group(1)
I'm not sure the point of asking you to write your own strip()
function is to delegate the task to the reg-ex engine. It seems like going out and buying a sledge hammer when the problem is to build a nut cracker.
$endgroup$
add a comment |
$begingroup$
If you call regex_strip(s, "")
, you will get:
re.error: unterminated character set at position 0
because neither ^[]
nor []$
is a value regular expression. You could avoid this by using if not chars:
instead of if chars == None:
.
There is no need to re.compile()
your regular expressions; you aren't saving the compiled patterns anywhere for re-use.
You can simplify your logic by using the reg-ex to capture the middle, non-stripped portion of the string, instead of doing two replacements for the start and end trim operations:
import re
def regex_strip(s, chars = None):
if chars:
trim = '[' + re.escape(chars) + ']*'
else:
trim = r's*'
return re.fullmatch(f"{trim}(.*?){trim}", s).group(1)
I'm not sure the point of asking you to write your own strip()
function is to delegate the task to the reg-ex engine. It seems like going out and buying a sledge hammer when the problem is to build a nut cracker.
$endgroup$
If you call regex_strip(s, "")
, you will get:
re.error: unterminated character set at position 0
because neither ^[]
nor []$
is a value regular expression. You could avoid this by using if not chars:
instead of if chars == None:
.
There is no need to re.compile()
your regular expressions; you aren't saving the compiled patterns anywhere for re-use.
You can simplify your logic by using the reg-ex to capture the middle, non-stripped portion of the string, instead of doing two replacements for the start and end trim operations:
import re
def regex_strip(s, chars = None):
if chars:
trim = '[' + re.escape(chars) + ']*'
else:
trim = r's*'
return re.fullmatch(f"{trim}(.*?){trim}", s).group(1)
I'm not sure the point of asking you to write your own strip()
function is to delegate the task to the reg-ex engine. It seems like going out and buying a sledge hammer when the problem is to build a nut cracker.
answered 8 hours ago
AJNeufeldAJNeufeld
8,7601831
8,7601831
add a comment |
add a comment |
$begingroup$
DRY. Both branches do identical re.sub
s. Take them out:
if chars == None:
strip_left = re.compile(r'^s*')
strip_right = re.compile(r's*$')
else:
strip_left = re.compile(r'^[' + re.escape(chars) + r']*')
strip_right = re.compile(r'[' + re.escape(chars) + r']*$')
s = re.sub(strip_left, "", s)
s = re.sub(strip_right, "", s)
return s
I recommend to go one step further, and unify the computation of strip_*
:
if chars == None:
chars = string.whitespace
strip_left = re.compile(r'^[' + re.escape(chars) + r']*')
strip_right = re.compile(r'[' + re.escape(chars) + r']*$')
s = re.sub(strip_left, "", s)
s = re.sub(strip_right, "", s)
return s
It is recommended to compare against None
as chars is None
rather than using ==
.
$endgroup$
add a comment |
$begingroup$
DRY. Both branches do identical re.sub
s. Take them out:
if chars == None:
strip_left = re.compile(r'^s*')
strip_right = re.compile(r's*$')
else:
strip_left = re.compile(r'^[' + re.escape(chars) + r']*')
strip_right = re.compile(r'[' + re.escape(chars) + r']*$')
s = re.sub(strip_left, "", s)
s = re.sub(strip_right, "", s)
return s
I recommend to go one step further, and unify the computation of strip_*
:
if chars == None:
chars = string.whitespace
strip_left = re.compile(r'^[' + re.escape(chars) + r']*')
strip_right = re.compile(r'[' + re.escape(chars) + r']*$')
s = re.sub(strip_left, "", s)
s = re.sub(strip_right, "", s)
return s
It is recommended to compare against None
as chars is None
rather than using ==
.
$endgroup$
add a comment |
$begingroup$
DRY. Both branches do identical re.sub
s. Take them out:
if chars == None:
strip_left = re.compile(r'^s*')
strip_right = re.compile(r's*$')
else:
strip_left = re.compile(r'^[' + re.escape(chars) + r']*')
strip_right = re.compile(r'[' + re.escape(chars) + r']*$')
s = re.sub(strip_left, "", s)
s = re.sub(strip_right, "", s)
return s
I recommend to go one step further, and unify the computation of strip_*
:
if chars == None:
chars = string.whitespace
strip_left = re.compile(r'^[' + re.escape(chars) + r']*')
strip_right = re.compile(r'[' + re.escape(chars) + r']*$')
s = re.sub(strip_left, "", s)
s = re.sub(strip_right, "", s)
return s
It is recommended to compare against None
as chars is None
rather than using ==
.
$endgroup$
DRY. Both branches do identical re.sub
s. Take them out:
if chars == None:
strip_left = re.compile(r'^s*')
strip_right = re.compile(r's*$')
else:
strip_left = re.compile(r'^[' + re.escape(chars) + r']*')
strip_right = re.compile(r'[' + re.escape(chars) + r']*$')
s = re.sub(strip_left, "", s)
s = re.sub(strip_right, "", s)
return s
I recommend to go one step further, and unify the computation of strip_*
:
if chars == None:
chars = string.whitespace
strip_left = re.compile(r'^[' + re.escape(chars) + r']*')
strip_right = re.compile(r'[' + re.escape(chars) + r']*$')
s = re.sub(strip_left, "", s)
s = re.sub(strip_right, "", s)
return s
It is recommended to compare against None
as chars is None
rather than using ==
.
answered 8 hours ago
vnpvnp
41.6k234106
41.6k234106
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
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%2fcodereview.stackexchange.com%2fquestions%2f222372%2fregex-version-of-strip-ch-7-automate-the-boring-stuff%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
$begingroup$
It all depends whether s includes all the white space characters. There are tons of them: en.wikipedia.org/wiki/Whitespace_character
$endgroup$
– dfhwze
9 hours ago