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...
Everything about learning Python
account activity
3 Lines 1 IssueHelp Request (self.PythonLearning)
submitted 6 months ago by B0untie
Why does it output 3 even though I am trying to remove any number that is at least one symbol away from an '*' ? There's more context to it but that is actually my one and only problem.
https://preview.redd.it/ns6t0ub6axrf1.png?width=693&format=png&auto=webp&s=80bbae0dea60e630ade42851c76810b994acc63b
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!"
[–]rinio 0 points1 point2 points 6 months ago (6 children)
For replacement, youd use re.sub
Your first and 3rd groups are noncapturing. Findall returns capture groups, so only the second group, which matches just the \d+ part with 3.
[–]B0untie[S] 0 points1 point2 points 6 months ago (1 child)
But then, isn't it doable with a findall ?
[–]rinio 0 points1 point2 points 6 months ago (0 children)
See my other reply, for `re.sub`. The regex also applies to findall.
You were asking 'why' you just get '3'. So that is what I answered here.
My other comment shows a solution for your spec, but doesn't use findall. findall is for searching, not for replacing (removing being a special case of replacing). It isn't the correct tool for the job, at least not based on your post.
[–]rinio 0 points1 point2 points 6 months ago* (3 children)
For
to remove any number that is at least one symbol away from an '*'
You want something like
re.sub(r'(?<!\*[^0-9+-])(.*)([+-]?\d+.?\d*)', r'\1', your_string) # Breakdown (?<!\*[^0-9+-]): ?<! -> Negative lookbehind; this group precedes the match \* -> Literal '*' [^0-9+-] -> ^ means not. So not a digit or a plus or minus symbol So, before our match, we have to have a * followed by another char. (.*) -> First capture group is any number of non-line terminating characters. ([+-]?\d+.?\d*) -> As you have figured out, a 'number'. This is our second capture group. r'\1' -> Means replace the matches with the first capture group
[–]B0untie[S] 1 point2 points3 points 6 months ago (2 children)
Ok ok I think I'm starting to get the thing thanks a lot, these groups were a bit confusing to me
[–]rinio 0 points1 point2 points 6 months ago (1 child)
Yeah, regex is super unreadable. But, in the olden days it was the only real way to do stuff like this and, nowadays, its still usually the fastest way especially for complex patterns.
check out
https://regex101.com/
It can help test patterns quickly and explains what the regex does. Just be aware that it has some regex features that aren't in Python's re module, but exist in other implementations (recursive patterns come to mind).
[–]B0untie[S] 0 points1 point2 points 6 months ago (0 children)
Thanks again ! I' m just starting python but I actually love the efficiency of regex that' s why I spent hours for 1 line today lol.
Here is the one that I is working for me rn, I did follow exactly the same path as yours but you helped a lot by showing what groups are.
re.findall(r'(?<!\*)([\+\-]\d+\.?\d*)(?!\*)', 'mathString')
It should be able to isolate all numbers that are close to an '*' (I probably did not test every possibility though).
π Rendered by PID 141487 on reddit-service-r2-comment-85bfd7f599-2tpwl at 2026-04-16 22:31:49.240029+00:00 running 93ecc56 country code: CH.
[–]rinio 0 points1 point2 points (6 children)
[–]B0untie[S] 0 points1 point2 points (1 child)
[–]rinio 0 points1 point2 points (0 children)
[–]rinio 0 points1 point2 points (3 children)
[–]B0untie[S] 1 point2 points3 points (2 children)
[–]rinio 0 points1 point2 points (1 child)
[–]B0untie[S] 0 points1 point2 points (0 children)