'checks the text for the presence of 2 or more characters or digits surrounded by parentheses, with at least the first character in uppercase
The contains_acronym
function checks the text for the presence of 2 or more characters or digits surrounded by parentheses, with at least the first character in uppercase (if it's a letter), returning True
if the condition is met, or False
otherwise. For example, "Instant messaging (IM) is a set of communication technologies used for text-based communication"
should return True
since (IM)
satisfies the match conditions. Fill in the regular expression in this function:
import re
def contains_acronym(text):
pattern = ___
result = re.search(pattern, text)
return result != None
print(contains_acronym("Instant messaging (IM) is a set of communication technologies used for text-based communication")) # True
print(contains_acronym("American Standard Code for Information Interchange (ASCII) is a character encoding standard for electronic communication")) # True
print(contains_acronym("Please do NOT enter without permission!")) # False
print(contains_acronym("PostScript is a fourth-generation programming language (4GL)")) # True
print(contains_acronym("Have fun using a self-contained underwater breathing apparatus (Scuba)!")) # True
I have tried with this pattern but it is not working with all given input cases:
pattern = r"\(([A-Z0-9_]+)\)"
Solution 1:[1]
Finally tried with below pattern and it covers all above scenarios with below code,
import re
def contains_acronym(text):
pattern = r"\([A-Za-z0-9]{2,}\)"
result = re.search(pattern, text)
return result != None
print(contains_acronym("Instant messaging (IM) is a set of communication technologies used for text-based communication")) # True
print(contains_acronym("American Standard Code for Information Interchange (ASCII) is a character encoding standard for electronic communication")) # True
print(contains_acronym("Please do NOT enter without permission!")) # False
print(contains_acronym("PostScript is a fourth-generation programming language (4GL)")) # True
print(contains_acronym("Have fun using a self-contained underwater breathing apparatus (Scuba)!")) # True
Solution 2:[2]
Use this as pattern
`pattern = r"\(\w.*\w\)" `
"w" means alphabet and number
Solution 3:[3]
import re
def contains_acronym(text):
pattern = r'\([A-Za-z0-9]{2,}\)'
result = re.search(pattern, text)
return result != None
print(contains_acronym("Instant messaging (IM) is a set of communication technologies used for text-based communication")) # True
print(contains_acronym("American Standard Code for Information Interchange (ASCII) is a character encoding standard for electronic communication")) # True
print(contains_acronym("Please do NOT enter without permission!")) # False
print(contains_acronym("PostScript is a fourth-generation programming language (4GL)")) # True
print(contains_acronym("Have fun using a self-contained underwater breathing apparatus (Scuba)!")) # True
Solution 4:[4]
import re
def contains_acronym(text):
pattern = r"\([A-Z0-9][A-Z0-9a-z]+\)"
result = re.search(pattern, text)
return result != None
Solution 5:[5]
Your regex is almost correct. You are just forgetting it has to have at least 2 so just make your first range a constant part of the string and repeat the same match with lowercase with +
(one or more):
pattern = r"\(([A-Z0-9_][A-Za-z0-9_]+)\)"
Solution 6:[6]
This should work
pattern = r"\([A-Z0-9].*\)"
Note that
- '+' is a metacharacter which matches one or more times.
- Should use '*'.
- Also double brackets are not required.
- \w also includes lowercase characters along with uppercase and integers.
Solution 7:[7]
pattern = r"\(\w{2,}\)"
This pattern works and looks better.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 | Aarju Raj Arya |
Solution 4 | SRS |
Solution 5 | Gino Mempin |
Solution 6 | shaviz soudager |
Solution 7 |