Regex confusion in python [duplicate] - python

This question already has answers here:
Reference - What does this regex mean?
(1 answer)
Regular expression: zero or more occurrences of optional character /
(1 answer)
Closed 4 years ago.
if i set the variable a to
a = "6253625879615786"
if i use the regex statement
print(re.findall(r'^[456][0-9]{15}[^,_]',a))
I do not get any output but if i use the regex statement with the * at the end to 0 or 1 times
print(re.findall(r'^[456][0-9]{15}[^,_]*',a))
Why is this?

Related

why is regular expression 'batRegex = re.compile(r'Bat(wo)+man') ' a bug? [duplicate]

This question already has answers here:
How do i repeat regex
(1 answer)
Free text parsing using long regex formula leading to error: multiple repeat in python? Screenshot included
(1 answer)
Closed 2 months ago.
I try to do this:
① batRegex = re.compile(r'Bat(wo)*+man')
and this:
mo3 = batRegex.search('My name is Batwowowowowowomanman.')
mo3.group()
And I expect this:
'Batwowowowowowoman'
But I got this when I type ①:
re.error: multiple repeat at position 8
I wonder what that means.
Because * means any times of repeat and + means at least one time repeat, that's exactly what happened in the mo3. What's wrong.
Thanks:)
My python version is Python 3.10.5.
In your regex Bat(wo)*+man, the * already means to repeat the group (wo) zero or more times. Therefore, the following + is out of place. You probably intended to use Bat(?:wo)+man. Here is an updated script:
batRegex = re.compile(r'Bat(?:wo)+man')
mo3 = batRegex.search('My name is Batwowowowowowomanman.')
print(mo3.group()) # Batwowowowowowoman

Python regular expression (matching in list) [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
Looking to extract only 7 digit numbers from this list that starts with the distance matrix, nothing after the underscore
The list:
['data_train_3366094.dump','agile_234444.pkl','distanceMatrix_1517144.dump', 'distanceMatrix_3366094_1.dump']
expecting output: 1517144 , 3366094
My guess is to explode(), separating with _ and .
Then match for numeric value

How to split a string into a list by skipping letters and ^ char [duplicate]

This question already has answers here:
How to extract numbers from a string in Python?
(19 answers)
extract digits in a simple way from a python string [duplicate]
(5 answers)
Closed 3 years ago.
I have the following string:
string_a = 81^A55
from which I'm trying to get the following list
string_a_int = [81,55]
I'm able to split the string into a list made by numbers as follows
list_only_number = re.split('[A-Z]+', string_a)
list_only_numbers = [81^, 55]
but I'm trying to figure out how to skip also the ^.
Any help would be much appreciated.

What does `, =` operator do? [duplicate]

This question already has answers here:
x, = ... - is this trailing comma the comma operator? [duplicate]
(2 answers)
Meaning of using commas and underscores with Python assignment operator? [duplicate]
(4 answers)
Closed 3 years ago.
What does this code do?
f = [1]
x, = f
x is set to 1 after doing this, but I cannot figure out what the , = does.

Is str.count() wrong? [duplicate]

This question already has answers here:
String count with overlapping occurrences [closed]
(25 answers)
How String.count() works? [duplicate]
(2 answers)
Python string count not working properly? [duplicate]
(2 answers)
Closed 4 years ago.
I'm having fun with some challenges and one of them makes me count substrings in a string. I have a problem specifically with "banana":
str = "banana"
print(str.count("ana"))
This should return 2 because "ana" appears two times:
b a n a n a
a n a
a n a
But str.count("ana") returns only 1. I've also tried with regexp:
import re
str = "banana"
print(len(re.findall("ana", str)))
But it also returns 1. Am I missing something?
thank you!
Yes, you are missing something.
str.count(): Return the number of (non-overlapping) occurrences of substring sub in string s

Categories