Python Regex stops after first "|" match [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
p = re.compile("[AG].{2}[ATG|ATA|AAG].{1}G")
regex_result = p.search('ZZZAXXATGXGZZZ')
regex_result.group()
'AXXATG'
I was expecting AXXATGXG instead.

Use a grouping construct (...) rather than a character class [...] around the alternatives:
p = re.compile("[AG].{2}(?:ATG|ATA|AAG).G")
^^^^^^^^^^^^^^^
The (?:ATG|ATA|AAG) matches 3 sequences: either a ATG, or ATA or AAG. The [ATG|ATA|AAG] character class matches 1 char, either A, T, G or |.
Note the {1} is redundant and can be removed.
Python:
import re
p = re.compile("[AG].{2}(?:ATG|ATA|AAG).G")
regex_result = p.search('ZZZAXXATGXGZZZ')
print(regex_result.group())
# => AXXATGXG
See IDEONE demo

Related

Adding sentence breaks to the beginning and end of each element in a list [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have a list of strings:
mini_corpus = ['I am Sam','Sam I am','I am Sam','I do not like green eggs and Sam']
I need to add a sentence boundary at the beginning and end of each element (i.e. 'BOS I am Sam EOS', 'BOS Sam I am EOS', etc.)
I've tried using map : mini_corpv2 = list(map(lambda x: 'BOS{}EOS'.format(x), mini_corpus)) but it throws 'list' object is not callable
Can anyone tell me what I'm doing wrong or suggest another method to implement this?
I suppose the problem is somewhere else. Your code runs without problems, resulting in
['BOSI am SamEOS',
'BOSSam I amEOS',
'BOSI am SamEOS',
'BOSI do not like green eggs and SamEOS']
(so you will probably want to add spaces after BOS and before EOS).
An alternative solution using list comprehension:
mini_corpv2 = [f'BOS {x} EOS' for x in mini_corpus]

Why am I getting two diferent outputs with my python code? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
def kaka(name):
r=''
for ch in name:
r=r+ch*3
return r
Output:
>>> kaka('Mississippi')
>>> 'MMMiiissssssiiissssssiiippppppiii'
But for this code:
def kaka(name):
for ch in name:
r=''
r=r+ch*3
return r
I am getting output as: iii
That's because in your second code you're re-assigning r back to the empty string ''. Thus you only get the final character multiplied 3 times (which for Mississippi is i).
You are getting 2 different outputs because in the first code you are initialising the value of r i.e r = '' outside the for loop and in the second program you are initialising value of r inside the for loop.

SyntaxError: invalid character in identifier [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
When executing with Python it shows error:
return (x * (1.0 — x))
^
SyntaxError: invalid character in identifier
How do I correct it?
Use the correct character for you minus operator: -. You are using some other 'dash' character that the interpreter is considering just a name like y or x. But it is invalid!
>>> bad_minus = "—"
>>> good_minus = "-"
>>> bad_minus == good_minus
False
>>> ord(good_minus)
45
>>> ord(bad_minus)
8212
>>>
Assuming the character between 1.0 and x is supposed to be a minus sign, replace it with an actual minus sign.
Your minus is not a minus. It's a "em dash".
Try replacing this '—' with '-'.

Python: How to store string inputs on a array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hi I would like to store strings on a array. This strings are produced in this loop:
while (count < ts ):
dt=tb
t1=count+180
t2=t1+360
dt1=dt+t1
dt2=dt+t2
slice=stream.slice(dt1, dt2)
B=str(dt1)
E=str(dt2)
slice.write(station+'_'+comp[i]+'_'+B+'_'+E, format="MSEED")
count = count + 360
bb=[]
name=station+B+'_'+E
a=[str(name)]
bb.append(a)
But it doesn't work. The variable name is from type:
name=2011-05-22T23:54:00.000000Z_2011-05-22T23:59:59.984000Z
And I would like to have an array like that:
bb=[2011-05-22T23:42:00.000000Z_2011-05-22T23:48:00.000000Z, 2011-05-22T23:48:00.000000Z_2011-05-22T23:54:00.000000Z, 2011-05-22T23:54:00.000000Z_2011-05-22T23:59:59.984000Z]
But what bb returns me is an array with the last element called:
bb=[2011-05-22T23:54:00.000000Z_2011-05-22T23:59:59.984000Z]
If I do it manually:
bb.append('2011-05-22T23:54:00.000000Z_2011-05-22T23:59:59.984000Z')
It works perfectly because I put the ''. But I need to it in a automatic way.
Any suggestion?
Thanks in advance!
Declare bb outside the loop and a will be a list. You will get a list of lists(not in the way you asked for)

Python sub string [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have run into an odd situation while trying to find a sub string in Python. I am aware that I should use the in operator.
My string looks like '(email#email.org, Name, ext)'.
When I run this in the interactive terminal, it starts to not match:
>>> '(foo#bar.org,' in a
True
>>> '(foo#bar.org, B' in a
False
I have the string exactly as the pattern is in the text I provide. I am just curious as to why in isn't working once it passes the first comma?
a is:
Purpose: foo - bar\n\n Server Admin: (baz#bar.org, a f. g, 6-6405) \n\n App Owner Group: hi\n\n App Owners: (blah, blah blah, 6-5627)\n (foo#bar.org, Brian Cody, 6-5624)\n\nNotes for Alerts:\n
Everything works as expected if a actually contains 'foo#bar.org, B':
>>> a = '(foo#bar.org, Bob, x1234)'
>>> 'foo#bar.org,' in a
True
>>> 'foo#bar.org, B' in a
True
>>>
The string that you provided actually has two spaces between (foo#bar.org, and Brian Cody. Therefore, your second expression will return False because it's looking for one and only one space.

Categories