-1

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_]+)\)"
Tomerikoo
  • 15,737
  • 15
  • 35
  • 52
Vinod
  • 1,154
  • 2
  • 19
  • 35
  • What have *you tried*, and what exactly is the problem with it? – jonrsharpe Sep 20 '20 at 11:42
  • need to find regex pattern for above problem statement and to fulfill output result for given sample inputs and @jonrsharpe is anything wrong in this question? – Vinod Sep 20 '20 at 11:48
  • No, you need to *write* a regex pattern for the above problem statement. It's your homework, not ours, you can't just dump it on SO. Maybe start with e.g. https://stackoverflow.com/q/4736/3001761. – jonrsharpe Sep 20 '20 at 11:48

7 Answers7

2

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
Vinod
  • 1,154
  • 2
  • 19
  • 35
2

Use this as pattern

`pattern = r"\(\w.*\w\)" `

"w" means alphabet and number

0

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_]+)\)"
Gino Mempin
  • 19,150
  • 23
  • 79
  • 104
0
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
0
import re
def contains_acronym(text):
  pattern = r"\([A-Z0-9][A-Z0-9a-z]+\)"
  result = re.search(pattern, text)
  return result != None
SRS
  • 1
0

This should work

 pattern = r"\([A-Z0-9].*\)"

Note that

  1. '+' is a metacharacter which matches one or more times.
  2. Should use '*'.
  3. Also double brackets are not required.
  4. \w also includes lowercase characters along with uppercase and integers.
0
pattern = r"\(\w{2,}\)"

This pattern works and looks better.

LimboKid
  • 1
  • 2