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
Im trying to gain an understanding of capture groups using this example:
sentence = "the quick brown fox jumps over the lazy dog"
re.search(r'\S+\s+\S+',sentence)
<_sre.SRE_Match object; span=(0, 9), match='the quick'>
I can see this matches as follows:
re.search(r'\S+\s+\S+',sentence).group()
'the quick'
I want to add a match group for the word 'quick' so I try this:
re.search(r'\S+\s+\(S+)',sentence)
Which gives an error:
error: unbalanced parenthesis at position 10
What am I doing wrong here?
Looks like a typo, but I'll still provide an explanation.
You are escaping the opening parenthesis making it matching a literal (, which makes the closing parenthesis at the end of the expression without an opening part, replace:
\S+\s+\(S+)
with:
\S+\s+(\S+)
Related
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 10 months ago.
Improve this question
This is my string:
Hair ReplacementHair BraidingHair Supplies & Accessories
my expected result should be:
Hair Replacement,Hair Braiding,Hair Supplies & Accessories
If two word like this ReplacementHair I want to split this two word and add comma between theme.
I tried this code:
re.sub(r"(\w)([A-Z])", r"\1 \2", text)
The above code splitting two word and add space between theme. I want comma instead of space.
You can replace the space in the replacement pattern with a comma.
import re
text = "Hair ReplacementHair BraidingHair Supplies & Accessories"
text2 = re.sub(r"(\w)([A-Z])", r"\1,\2", text)
print(text2)
output
Hair Replacement,Hair Braiding,Hair Supplies & Accessories
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]
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
I have this regex pattern:
(?P<prefix>.*)(?<!\\)\((?P<words>.+)(?<!\\)\)(?P<postfix>.*)
This regex is supposed to match a string like this:
hello my (friend|enemy) nice to see you again
The prefix group should capture hello my.
The words group should capture friend|enemy.
The postfix group should capture nice to see you again
This regex also uses lookbehinds to check if ( and ) are escaped using \ in string. For example, these two samples should not be detected since there is a \ before ( and ):
hello my \(friend|enemy) nice to see you again
hello my (friend|enemy\) nice to see you again
This pattern works well when I check it using online websites but when I try to run in in python (I'm using python 3.7), it throws the following error:
re.error: missing ), unterminated subpattern at position 35
What is the problem?
Edit:
Here is how I use it in python:
pattern = "(?P<prefix>.*)(?<!\\)\((?P<words>.+)(?<!\\)\)(?P<postfix>.*)"
match = re.search(pattern, line)
#Md Narimani
as #erhumoro suggested in comments, instead of:
line = "hello my (friend|enemy) nice to see you again"
pattern = "(?P<prefix>.*)(?<!\\)\((?P<words>.+)(?<!\\)\)(?P<postfix>.*)"
match = re.search(pattern, line)
Do:
line = "hello my (friend|enemy) nice to see you again"
pattern = r"(?P<prefix>.*)(?<!\\)\((?P<words>.+)(?<!\\)\)(?P<postfix>.*)"
match = re.search(pattern, line)
It is because of problems with escaping characters.
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
import random
for i in range(1,21):
print("%10d" %(random.randrange(1,7)),
if (i % 5 == 0):
print ("")
What is wrong in this code ?
I know basic python (almost), but i am not able to figure out what could be error in this program.
it is showing this error:
Syntax Error: invalid syntax at line 6 (if statement)
The fourth line is missing bracket .. Thanks all of you
You missed a right bracket )
print("%10d" %(random.randrange(1,7))),
would be correct
You're missing a ) before the last comma on line 4.
Your parenthesis on line 4 don't match. Because you have an unclosed paren, python doesn't report this syntax error until the colon on line 6 (python ignores line breaks as long as you are inside an enclosing set of parenthesis, brackets, or braces).
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.