Python regex needed for format: 'delete([any text here])'


Python regex needed for format: 'delete([any text here])'



I am a total regex beginner. I want to create a regular expression that strictly allows the word delete followed by two closed parenthesis that contain any kind of characters (http://www.waynesworld1.com).


delete


(http://www.waynesworld1.com)



If I put it all together, it should accept the following: delete(http://www.waynesworld123.com).


delete(http://www.waynesworld123.com)



Let me emphasize that the regex should strictly accept delete() and shouldn't accept elete(). As long as the user types in delete() anything is acceptable within the parenthesis (example: this would be fine delete(12!@Ww)


delete()


elete()


delete()


delete(12!@Ww)



How can I craft this regex in Python? So far all I have is /delete/ for my regex.


/delete/





Why the downvote? This is a perfectly reasonable question that is well constructed and very understandable. I have done previous research and haven't found anything that works.
– Erik Åsland
May 24 '16 at 18:39





There is someone on this site who downvotes all python regex questions, as if there should be none. I assume this person thinks that no regex questions are worth asking because whoever asks should have done more research. I have seen this over and over. It is infuriating, but I don't think there is anything we can do about it.
– Chris Nielsen
May 24 '16 at 18:51






I would guess that the existing downvote is because the question doesn't show any evidence of an attempt to learn to write regexes, and it's asking for a regex rather than information about regex-construction techniques. Regexes are code, and you're asking for code here. People don't like that.
– user2357112
May 24 '16 at 18:54





"doesn't show any evidence of an attempt to learn to write regexes". Wow. How would you know that from just reading the question posed? From personal experience, regex can be extremely frustrating and complicated. When someone provides the code solution to the question, it is a very valuable learning tool. A person can study regex for a long time and still need help with something that seems like it should have been easy. Regex has a steep learning curve. Would you prefer that everybody who posts a regex question explicitly state how much time they spent studying it? Seems pointless.
– Chris Nielsen
May 24 '16 at 19:00






@ChrisNielsen I think that there is a big difference between "Can I have some code" and "Here's what I found so far, I've hit a roadblock, and can I have some help". Keep in mind that these 2 hypothetical people could have the exact same problem, and researched the exact same thing. The difference lies in their attitude and effort put into the question (No offence to the OP, whose question is for the most part fine). Speaking from personal experience, it helps potential question answerers if they know a little about what the OP knows so they can provide better and more relevant answers.
– Mistah Figgins
May 24 '16 at 20:16




1 Answer
1



Here you go:


^delete(.*)$

^ assert position at start of the string
delete matches the characters delete literally (case sensitive)
( matches the character ( literally
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
) matches the character ) literally
$ assert position at end of the string



Here is some Python test code:


import re

txt= {"delete(http://www.waynesworld123.com)",
"delete(12!@Ww)",
"elete(test)",
"delete[test]",
"test"}


pattern=re.compile('^delete(.*)$', re.DOTALL)

for line in txt:
if pattern.search(line):
print 'PASS', line
else:
print 'FAIL',line





Thanks Rudy, this works just fine. I will give you the answer in 9 minutes when Stack Overflow allows me to.
– Erik Åsland
May 24 '16 at 18:38





I dont think having the start and end assertions are necessary since this is mostly likely going to be used as a findall (and OP does not state this criteria). It would also do well to add a ? after your .* token to avoid greedy grabs. or changing the token to something like [^)]*
– R Nar
May 24 '16 at 18:40


?


.*


[^)]*





It looked like he was asking for validation of an input - "Allows". If he confirms that he is looking for a search, I can update. Thanks for the comment.
– user5684647
May 24 '16 at 18:42






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API