python-3.x 检查文本中是否存在两个或多个用括号括起来的字符或数字,并且至少第一个字符是大写的

0dxa2lsx  于 2022-12-24  发布在  Python
关注(0)|答案(9)|浏览(122)

contains_acronym函数检查文本中是否存在两个或更多个用括号括起来的字符或数字,并且至少第一个字符是大写的(如果是字母),如果满足条件则返回True,否则返回False。例如,"Instant messaging (IM) is a set of communication technologies used for text-based communication"应返回True,因为(IM)满足匹配条件。请在此函数中填写正则表达式:

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

我已经尝试过这个模式,但是它并不适用于所有给定的输入情况:

pattern = r"\(([A-Z0-9_]+)\)"
s4chpxco

s4chpxco1#

最后尝试使用以下模式,它使用以下代码涵盖了上述所有场景,

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
bfhwhh0e

bfhwhh0e2#

将此用作模式

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

“w”表示字母和数字

up9lanfz

up9lanfz3#

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
5gfr0r5j

5gfr0r5j4#

import re
def contains_acronym(text):
  pattern = r"\([A-Z0-9][A-Z0-9a-z]+\)"
  result = re.search(pattern, text)
  return result != None
drnojrws

drnojrws5#

你的正则表达式几乎是正确的,你只是忘记了它必须有至少2个**,所以只要把你的第一个范围作为字符串的常量部分,然后用小写的+(一个或多个)重复同样的匹配:

pattern = r"\(([A-Z0-9_][A-Za-z0-9_]+)\)"
mklgxw1f

mklgxw1f6#

∮这应该行得通∮

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

请注意
1."+"是匹配一次或多次的元字符。
1.应使用"*"。
1.也不需要双括号。
1.\w还包括小写字符、大写字符和整数。

watbbzwu

watbbzwu7#

pattern = r"\(\w{2,}\)"

这个图案很好用,看起来也更好。

xqnpmsa8

xqnpmsa88#

模式= r”([A-Z 0 -9])*”
这个很适合我,很简单

svujldwt

svujldwt9#

这个怎么样?

pattern = '\(\w+\)'

相关问题