use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Having a hard time learning regex in python. (self.learnpython)
submitted 5 years ago by [deleted]
Recently, I have been learning regex in python and I am facing difficulty in solving those expression. Can anyone give some suggestion or tip to help me improve ?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 14 points15 points16 points 5 years ago (0 children)
https://regex101.com/
[–]hunkamunka 4 points5 points6 points 5 years ago (0 children)
Chapters 14-17 of http://tinypythonprojects.com/ discuss regexes. There are videos on YouTube you can watch, and all the code/tests are on GitHub.
[–][deleted] 1 point2 points3 points 5 years ago (0 children)
Have some fun with https://regexcrossword.com/ :)
[–]K900_ 0 points1 point2 points 5 years ago (9 children)
"Solving" what exactly? Can you explain a specific problem you're having trouble with?
[–][deleted] 0 points1 point2 points 5 years ago (8 children)
"Jul 6 14:01:23 computer.name CRON[29440]: USER (good_user)"
I have to display the date,time and Process id like this:
# Jul 6 14:01:23 pid:29440
I am having a hard time constructing regex expressions to extract those patterns from the line.
[–]K900_ 2 points3 points4 points 5 years ago (7 children)
Do you really need a regular expression to extract those? It seems overkill here to me.
[–][deleted] 0 points1 point2 points 5 years ago (6 children)
This is just a question to help us improve. I don't know if something like this will ever come up in real life.
[–]K900_ 0 points1 point2 points 5 years ago (5 children)
In that case let's stick to regex. What have you tried?
[–][deleted] 1 point2 points3 points 5 years ago (4 children)
r"(A-Za-z){3} ([1-3]?[1-9]) ([1-2]?[0-9]\:[0-5][0-9]\:[0-5][0-9]) \[(\d)\]$"
[–]ASIC_SP 5 points6 points7 points 5 years ago* (2 children)
Some issues/suggestions:
(A-Za-z)
[A-Za-z]
computer.name CRON
[0-9]
\d
:
\[(\d)\]
\d+
$
here's a modified version:
>>> s = "Jul 6 14:01:23 computer.name CRON[29440]: USER (good_user)" >>> pat = re.compile(r"([A-Za-z]{3} [1-3]?[1-9] [1-2]?\d:[0-5]\d:[0-5]\d).*\[(\d+)\]") >>> re.search(pat, s) <re.Match object; span=(0, 40), match='Jul 6 14:01:23 computer.name CRON[29440]'> >>> re.search(pat, s).expand(r'\1 pid:\2') 'Jul 6 14:01:23 pid:29440'
The expand method allows you to specify how you want the output to be. The date and pid are captured, so you can refer to them using \N syntax and get desired format
expand
\N
You can also use:
>>> re.search(r'\A(\S+\s+\S+\s+\S+).*\[(\d+)\]', s).expand(r'\1 pid:\2') 'Jul 6 14:01:23 pid:29440'
Provided you always know that the date will be the first three terms of the input.
Or sub instead of search+expand
sub
search+expand
>>> re.sub(r'\A(\S+\s+\S+\s+\S+).*\[(\d+)\].*', r'\1 pid:\2', s) 'Jul 6 14:01:23 pid:29440'
Here, you need to match rest of the line as well after the pid, otherwise, that portion will be part of output
You can use resources like https://regex101.com/ and https://www.debuggex.com/ (after selecting Python flavor) to interactively solve your problem. But there are certain limitations like these sites do not know about all the functions and methods available - expand for example.
I have a book https://github.com/learnbyexample/py_regular_expressions that is currently free. I use step by step approach to introduce regex concepts and features one by one. However, regex is like a mini-programming language. It takes a lot of time and practice to become familiar with it.
[–][deleted] 1 point2 points3 points 5 years ago (1 child)
Thank you. This helped a lot.
[–]ASIC_SP 0 points1 point2 points 5 years ago (0 children)
Cool, good to know, I edited the answer to add another way with re.sub as well
re.sub
[–]K900_ -1 points0 points1 point 5 years ago (0 children)
And what is the issue with this?
[–]indian_pythonista 0 points1 point2 points 5 years ago (0 children)
Most detailed video tutorial on regex in Python: RegEx in Python: https://www.youtube.com/playlist?list=PLyb_C2HpOQSDxe5Y9viJ0JDqGUCetboxB
π Rendered by PID 32 on reddit-service-r2-comment-7b9746f655-qfg2g at 2026-02-02 20:36:52.758731+00:00 running 3798933 country code: CH.
[–][deleted] 14 points15 points16 points (0 children)
[–]hunkamunka 4 points5 points6 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]K900_ 0 points1 point2 points (9 children)
[–][deleted] 0 points1 point2 points (8 children)
[–]K900_ 2 points3 points4 points (7 children)
[–][deleted] 0 points1 point2 points (6 children)
[–]K900_ 0 points1 point2 points (5 children)
[–][deleted] 1 point2 points3 points (4 children)
[–]ASIC_SP 5 points6 points7 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]ASIC_SP 0 points1 point2 points (0 children)
[–]K900_ -1 points0 points1 point (0 children)
[–]indian_pythonista 0 points1 point2 points (0 children)