Reputation: 1888
I have a column called `cc_flags' in a Python astropy table. It looks like::
0000
ddOO
0000
hHOO
0000
DD00
hHOO
hHPO
P000
00h0
...
0000
I'd like to pull out every row with the last character of the four if it is a '0'., i.e a '***0' or a '???0' matcher.
I'm getting an
error: nothing to repeat at position 0
with
import re
regex = r"???0"
re.findall(regex,data['cc_flags'])
What am I doing wrong?!
Upvotes: 0
Views: 2725
Reputation: 1809
The issue is with regex not escaping the special characters in your search term. To get this to work, simply change your statement to the following:
import re
regex = re.escape("???0")
re.findall(regex,data['cc_flags'])
data['cc_flags']
should be a string, not a list.
Upvotes: 0
Reputation: 43534
As others have said, you need to use .
instead of ?
. Another way is to use {}
to specify how many matches.
For example, try this:
import re
regex = r".{3}0"
re.findall(regex,data['cc_flags'])
.
means match any character (except for line terminators){3}
means match exactly 3 timesUpvotes: 1
Reputation: 15397
If you're not limited to regex or regex like, this is simple:
has_zeros = [line for line in data['cc_flags'] if line[-1] == '0']
This list comprehension will extract out each element from data['cc_flags']
into a variable named line, and return a resulting list that is only comprised of the last character of line being a 0.
(Obviously, if you need your if statement to be more specific, you can add whatever else you need into the filter condition.)
Upvotes: 2
Reputation: 2086
?
does not mean "any character" in regex. Swap your ?
s for .
s
Upvotes: 3