繁体   English   中英

如何将字符串与特定字符匹配

[英]How to match a string with specific characters

如何使用正则表达式匹配以 $ 结尾的字符串?

美国历史$或韩国历史$或英国$

我想匹配忽略除以 $ 结尾以外的任何其他内容

我试过

pattern = re.compile(r'\w\$')
if pattern.findall(s):
    print('Found')
else
    print('Not found')

但我认为空白给了我一个错误。 我该怎么办?

尝试

( # match group
  \w+ # match one or more AZ-az
  \$ # must end with $
)
  
pattern = re.compile(r'(\w+\$)')
if pattern.findall(s):
    print('Found')
else
    print('Not found')

如果要添加数字,请使用 ([\\w\\d]+$)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM