簡體   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