-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
  • 18,379
  • 16
  • 47
  • 61
Vinod
  • 1,234
  • 3
  • 21
  • 37
  • 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

9 Answers9

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,234
  • 3
  • 21
  • 37
2

Use this as pattern

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

"w" means alphabet and number.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
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
  • 25,369
  • 29
  • 96
  • 135
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
0
pattern = r"\([A-Z0-9]\)*"

This one works for me. Pretty simple.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • you regex says, that there may be only uppercase characters within the parantheses. But after the first `[A-Z0-0]` there may be other characters. Then there must be at least 2 ore more. `*` say "0 or more"... – Andy A. Jun 15 '22 at 06:26
  • Welcome to Stack Overflow! Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the [help center](https://stackoverflow.com/help/how-to-answer). – Ethan Jun 17 '22 at 13:50
0

Escape the opening and closing brackets, define the first character to be a number or uppercase characters, then subsequent characters as uppercase, lowercase, or numeric. I use a * after this to show this can be repeated as many times or not at all. i.e. you'll have the correct output with (IM) and with (I) as well.

pattern = "\([A-Z0-9][A-Za-z0-9]*\)"