Apply list of regex pattern on list python





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







10















I have data frame in which txt column contains a list. I want to clean the txt column using function clean_text().



data = {'value':['abc.txt', 'cda.txt'], 'txt':['[''2019/01/31-11:56:23.288258 1886     7F0ED4CDC704     asfasnfs: remove datepart'']',
'[''2019/02/01-11:56:23.288258 1886 7F0ED4CDC704 asfasnfs: remove datepart'']']}
df = pandas.DataFrame(data=data)

def clean_text(text):
"""
:param text: it is the plain text
:return: cleaned text
"""
patterns = [r"^{53}",
r"[A-Za-z]+[d]+[w]*|[d]+[A-Za-z]+[w]*",
r"[-=/':,?${}[]-_()>.~" ";+]"]

for p in patterns:
text = re.sub(p, '', text)

return text


My Solution:



df['txt'] = df['txt'].apply(lambda x: clean_text(x))


But I am getting below error:
Error



sre_constants.error: nothing to repeat at position 1









share|improve this question

























  • Possible duplicate of Regex sre_constants.error: bad character range

    – sophros
    Feb 10 at 19:59











  • @sophros, this question is different.

    – user15051990
    Feb 10 at 20:07











  • in what way it is different? The error is the same.

    – sophros
    Feb 10 at 20:12


















10















I have data frame in which txt column contains a list. I want to clean the txt column using function clean_text().



data = {'value':['abc.txt', 'cda.txt'], 'txt':['[''2019/01/31-11:56:23.288258 1886     7F0ED4CDC704     asfasnfs: remove datepart'']',
'[''2019/02/01-11:56:23.288258 1886 7F0ED4CDC704 asfasnfs: remove datepart'']']}
df = pandas.DataFrame(data=data)

def clean_text(text):
"""
:param text: it is the plain text
:return: cleaned text
"""
patterns = [r"^{53}",
r"[A-Za-z]+[d]+[w]*|[d]+[A-Za-z]+[w]*",
r"[-=/':,?${}[]-_()>.~" ";+]"]

for p in patterns:
text = re.sub(p, '', text)

return text


My Solution:



df['txt'] = df['txt'].apply(lambda x: clean_text(x))


But I am getting below error:
Error



sre_constants.error: nothing to repeat at position 1









share|improve this question

























  • Possible duplicate of Regex sre_constants.error: bad character range

    – sophros
    Feb 10 at 19:59











  • @sophros, this question is different.

    – user15051990
    Feb 10 at 20:07











  • in what way it is different? The error is the same.

    – sophros
    Feb 10 at 20:12














10












10








10


1






I have data frame in which txt column contains a list. I want to clean the txt column using function clean_text().



data = {'value':['abc.txt', 'cda.txt'], 'txt':['[''2019/01/31-11:56:23.288258 1886     7F0ED4CDC704     asfasnfs: remove datepart'']',
'[''2019/02/01-11:56:23.288258 1886 7F0ED4CDC704 asfasnfs: remove datepart'']']}
df = pandas.DataFrame(data=data)

def clean_text(text):
"""
:param text: it is the plain text
:return: cleaned text
"""
patterns = [r"^{53}",
r"[A-Za-z]+[d]+[w]*|[d]+[A-Za-z]+[w]*",
r"[-=/':,?${}[]-_()>.~" ";+]"]

for p in patterns:
text = re.sub(p, '', text)

return text


My Solution:



df['txt'] = df['txt'].apply(lambda x: clean_text(x))


But I am getting below error:
Error



sre_constants.error: nothing to repeat at position 1









share|improve this question
















I have data frame in which txt column contains a list. I want to clean the txt column using function clean_text().



data = {'value':['abc.txt', 'cda.txt'], 'txt':['[''2019/01/31-11:56:23.288258 1886     7F0ED4CDC704     asfasnfs: remove datepart'']',
'[''2019/02/01-11:56:23.288258 1886 7F0ED4CDC704 asfasnfs: remove datepart'']']}
df = pandas.DataFrame(data=data)

def clean_text(text):
"""
:param text: it is the plain text
:return: cleaned text
"""
patterns = [r"^{53}",
r"[A-Za-z]+[d]+[w]*|[d]+[A-Za-z]+[w]*",
r"[-=/':,?${}[]-_()>.~" ";+]"]

for p in patterns:
text = re.sub(p, '', text)

return text


My Solution:



df['txt'] = df['txt'].apply(lambda x: clean_text(x))


But I am getting below error:
Error



sre_constants.error: nothing to repeat at position 1






python regex list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 10 at 22:42









coldspeed

141k25157245




141k25157245










asked Feb 10 at 19:34









user15051990user15051990

710717




710717













  • Possible duplicate of Regex sre_constants.error: bad character range

    – sophros
    Feb 10 at 19:59











  • @sophros, this question is different.

    – user15051990
    Feb 10 at 20:07











  • in what way it is different? The error is the same.

    – sophros
    Feb 10 at 20:12



















  • Possible duplicate of Regex sre_constants.error: bad character range

    – sophros
    Feb 10 at 19:59











  • @sophros, this question is different.

    – user15051990
    Feb 10 at 20:07











  • in what way it is different? The error is the same.

    – sophros
    Feb 10 at 20:12

















Possible duplicate of Regex sre_constants.error: bad character range

– sophros
Feb 10 at 19:59





Possible duplicate of Regex sre_constants.error: bad character range

– sophros
Feb 10 at 19:59













@sophros, this question is different.

– user15051990
Feb 10 at 20:07





@sophros, this question is different.

– user15051990
Feb 10 at 20:07













in what way it is different? The error is the same.

– sophros
Feb 10 at 20:12





in what way it is different? The error is the same.

– sophros
Feb 10 at 20:12












2 Answers
2






active

oldest

votes


















10














^{53} is not a valid regular expression, since the repeater {53} must be preceded by a character or a pattern that can be repeated. If you mean to make it validate a string that is at least 53 characters long you can use the following pattern instead:



^.{53}





share|improve this answer



















  • 1





    Thanks for answer. I have updated question, now I get Attribute error.

    – user15051990
    Feb 10 at 19:59






  • 1





    @user15051990 Does ^.{53,} work?

    – MilkyWay90
    Feb 10 at 23:38



















3














The culprit is the first pattern from the list - r"^{53}". It reads: ^ - match the beginning of the string and then {53} repeat the previous character or group 53 times. Wait... but there is no other character than ^ which cannot be repeated! Indeed. Add a char that you want to match 53 repetitions of. Or, escape the sequence {53} if you want to match it verbatim, e.g. using re.escape.






share|improve this answer
























  • Thanks for answer. I have updated question, now I get Attribute error.

    – user15051990
    Feb 10 at 19:59






  • 2





    This should really be another question. How a reader of the question can make any sense of the answers if you change the crucial elements of the question?

    – sophros
    Feb 10 at 20:08











  • Alright I will post it as different question.

    – user15051990
    Feb 10 at 20:09






  • 1





    And before you do that - please revert the change first so that the answers make sense with the question.

    – sophros
    Feb 10 at 20:11






  • 2





    I have already done that although I believe you should reward the effort already made on answering this question.

    – sophros
    Feb 10 at 20:16












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%2f54620212%2fapply-list-of-regex-pattern-on-list-python%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









10














^{53} is not a valid regular expression, since the repeater {53} must be preceded by a character or a pattern that can be repeated. If you mean to make it validate a string that is at least 53 characters long you can use the following pattern instead:



^.{53}





share|improve this answer



















  • 1





    Thanks for answer. I have updated question, now I get Attribute error.

    – user15051990
    Feb 10 at 19:59






  • 1





    @user15051990 Does ^.{53,} work?

    – MilkyWay90
    Feb 10 at 23:38
















10














^{53} is not a valid regular expression, since the repeater {53} must be preceded by a character or a pattern that can be repeated. If you mean to make it validate a string that is at least 53 characters long you can use the following pattern instead:



^.{53}





share|improve this answer



















  • 1





    Thanks for answer. I have updated question, now I get Attribute error.

    – user15051990
    Feb 10 at 19:59






  • 1





    @user15051990 Does ^.{53,} work?

    – MilkyWay90
    Feb 10 at 23:38














10












10








10







^{53} is not a valid regular expression, since the repeater {53} must be preceded by a character or a pattern that can be repeated. If you mean to make it validate a string that is at least 53 characters long you can use the following pattern instead:



^.{53}





share|improve this answer













^{53} is not a valid regular expression, since the repeater {53} must be preceded by a character or a pattern that can be repeated. If you mean to make it validate a string that is at least 53 characters long you can use the following pattern instead:



^.{53}






share|improve this answer












share|improve this answer



share|improve this answer










answered Feb 10 at 19:54









blhsingblhsing

43.5k41744




43.5k41744








  • 1





    Thanks for answer. I have updated question, now I get Attribute error.

    – user15051990
    Feb 10 at 19:59






  • 1





    @user15051990 Does ^.{53,} work?

    – MilkyWay90
    Feb 10 at 23:38














  • 1





    Thanks for answer. I have updated question, now I get Attribute error.

    – user15051990
    Feb 10 at 19:59






  • 1





    @user15051990 Does ^.{53,} work?

    – MilkyWay90
    Feb 10 at 23:38








1




1





Thanks for answer. I have updated question, now I get Attribute error.

– user15051990
Feb 10 at 19:59





Thanks for answer. I have updated question, now I get Attribute error.

– user15051990
Feb 10 at 19:59




1




1





@user15051990 Does ^.{53,} work?

– MilkyWay90
Feb 10 at 23:38





@user15051990 Does ^.{53,} work?

– MilkyWay90
Feb 10 at 23:38













3














The culprit is the first pattern from the list - r"^{53}". It reads: ^ - match the beginning of the string and then {53} repeat the previous character or group 53 times. Wait... but there is no other character than ^ which cannot be repeated! Indeed. Add a char that you want to match 53 repetitions of. Or, escape the sequence {53} if you want to match it verbatim, e.g. using re.escape.






share|improve this answer
























  • Thanks for answer. I have updated question, now I get Attribute error.

    – user15051990
    Feb 10 at 19:59






  • 2





    This should really be another question. How a reader of the question can make any sense of the answers if you change the crucial elements of the question?

    – sophros
    Feb 10 at 20:08











  • Alright I will post it as different question.

    – user15051990
    Feb 10 at 20:09






  • 1





    And before you do that - please revert the change first so that the answers make sense with the question.

    – sophros
    Feb 10 at 20:11






  • 2





    I have already done that although I believe you should reward the effort already made on answering this question.

    – sophros
    Feb 10 at 20:16
















3














The culprit is the first pattern from the list - r"^{53}". It reads: ^ - match the beginning of the string and then {53} repeat the previous character or group 53 times. Wait... but there is no other character than ^ which cannot be repeated! Indeed. Add a char that you want to match 53 repetitions of. Or, escape the sequence {53} if you want to match it verbatim, e.g. using re.escape.






share|improve this answer
























  • Thanks for answer. I have updated question, now I get Attribute error.

    – user15051990
    Feb 10 at 19:59






  • 2





    This should really be another question. How a reader of the question can make any sense of the answers if you change the crucial elements of the question?

    – sophros
    Feb 10 at 20:08











  • Alright I will post it as different question.

    – user15051990
    Feb 10 at 20:09






  • 1





    And before you do that - please revert the change first so that the answers make sense with the question.

    – sophros
    Feb 10 at 20:11






  • 2





    I have already done that although I believe you should reward the effort already made on answering this question.

    – sophros
    Feb 10 at 20:16














3












3








3







The culprit is the first pattern from the list - r"^{53}". It reads: ^ - match the beginning of the string and then {53} repeat the previous character or group 53 times. Wait... but there is no other character than ^ which cannot be repeated! Indeed. Add a char that you want to match 53 repetitions of. Or, escape the sequence {53} if you want to match it verbatim, e.g. using re.escape.






share|improve this answer













The culprit is the first pattern from the list - r"^{53}". It reads: ^ - match the beginning of the string and then {53} repeat the previous character or group 53 times. Wait... but there is no other character than ^ which cannot be repeated! Indeed. Add a char that you want to match 53 repetitions of. Or, escape the sequence {53} if you want to match it verbatim, e.g. using re.escape.







share|improve this answer












share|improve this answer



share|improve this answer










answered Feb 10 at 19:57









sophrossophros

2,98411134




2,98411134













  • Thanks for answer. I have updated question, now I get Attribute error.

    – user15051990
    Feb 10 at 19:59






  • 2





    This should really be another question. How a reader of the question can make any sense of the answers if you change the crucial elements of the question?

    – sophros
    Feb 10 at 20:08











  • Alright I will post it as different question.

    – user15051990
    Feb 10 at 20:09






  • 1





    And before you do that - please revert the change first so that the answers make sense with the question.

    – sophros
    Feb 10 at 20:11






  • 2





    I have already done that although I believe you should reward the effort already made on answering this question.

    – sophros
    Feb 10 at 20:16



















  • Thanks for answer. I have updated question, now I get Attribute error.

    – user15051990
    Feb 10 at 19:59






  • 2





    This should really be another question. How a reader of the question can make any sense of the answers if you change the crucial elements of the question?

    – sophros
    Feb 10 at 20:08











  • Alright I will post it as different question.

    – user15051990
    Feb 10 at 20:09






  • 1





    And before you do that - please revert the change first so that the answers make sense with the question.

    – sophros
    Feb 10 at 20:11






  • 2





    I have already done that although I believe you should reward the effort already made on answering this question.

    – sophros
    Feb 10 at 20:16

















Thanks for answer. I have updated question, now I get Attribute error.

– user15051990
Feb 10 at 19:59





Thanks for answer. I have updated question, now I get Attribute error.

– user15051990
Feb 10 at 19:59




2




2





This should really be another question. How a reader of the question can make any sense of the answers if you change the crucial elements of the question?

– sophros
Feb 10 at 20:08





This should really be another question. How a reader of the question can make any sense of the answers if you change the crucial elements of the question?

– sophros
Feb 10 at 20:08













Alright I will post it as different question.

– user15051990
Feb 10 at 20:09





Alright I will post it as different question.

– user15051990
Feb 10 at 20:09




1




1





And before you do that - please revert the change first so that the answers make sense with the question.

– sophros
Feb 10 at 20:11





And before you do that - please revert the change first so that the answers make sense with the question.

– sophros
Feb 10 at 20:11




2




2





I have already done that although I believe you should reward the effort already made on answering this question.

– sophros
Feb 10 at 20:16





I have already done that although I believe you should reward the effort already made on answering this question.

– sophros
Feb 10 at 20:16


















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%2f54620212%2fapply-list-of-regex-pattern-on-list-python%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

Human spaceflight

Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

張江高科駅