簡體   English   中英

python 正則表達式重新匹配

[英]python regex re.match

import re

list=["22.11","23","66.7f","123abcde","Case44","Happy","78","66.7","yes123","Book111"]

def removeInt(list):
    
for str in list:
            
    if re.match("\d+",str):
       
           result=re.search("\d+",str)
           print("Original sentence:", str)
           print("Found integer",result.group(0),"at the beginning of the string, starting index",result.start(),"and ending index at index",result.end(),".The rest of the string is:"**???????????**)
  

removeInt(list)

嗨,所以我目前正在研究 python 正則表達式練習,以上是我現在的代碼。 在 output 的末尾,如果我想在從中刪除 integer 后顯示字符串的 rest。 我可以使用什么 function?

這是 output 的示例:

Original sentence: “90years” 
Found integer 90 at the beginning of this string, starting at index _ ending at index _. The rest of the string is: years.

您可能會發現使用組來捕獲數字和字符串前后的部分更容易。 注意我假設您不想在第一組之后匹配其他數字:

import re

list=["22.11","23","66.7f","123abcde","Case44","Happy","78","66.7","yes123","Book111"]

def removeInt(list):
    for str in list:
        result = re.match("([^\d]*)(\d+)(.*)",str)
        if result is not None:
            print("Original sentence:", str)
            print("Found integer",
                  result.group(2),
                  "in the string, starting at index",
                  result.start(2),
                  "and ending at index",
                  result.end(2),
                  ". The rest of the string is:", result.group(1)+result.group(3))
  
removeInt(list)

Output:

Original sentence: 22.11
Found integer 22 in the string, starting at index 0 and ending at index 2 . The rest of the string is: .11
Original sentence: 23
Found integer 23 in the string, starting at index 0 and ending at index 2 . The rest of the string is: 
Original sentence: 66.7f
Found integer 66 in the string, starting at index 0 and ending at index 2 . The rest of the string is: .7f
Original sentence: 123abcde
Found integer 123 in the string, starting at index 0 and ending at index 3 . The rest of the string is: abcde
Original sentence: Case44
Found integer 44 in the string, starting at index 4 and ending at index 6 . The rest of the string is: Case
Original sentence: 78
Found integer 78 in the string, starting at index 0 and ending at index 2 . The rest of the string is: 
Original sentence: 66.7
Found integer 66 in the string, starting at index 0 and ending at index 2 . The rest of the string is: .7
Original sentence: yes123
Found integer 123 in the string, starting at index 3 and ending at index 6 . The rest of the string is: yes
Original sentence: Book111
Found integer 111 in the string, starting at index 4 and ending at index 7 . The rest of the string is: Book

我猜你可以使用 .find() 字符串方法。

find() 方法返回 substring 的第一次出現的索引(如果找到)。 如果未找到,則返回 -1。

find() 方法的語法是:

str.find(sub, start, end)

在哪里:

  • sub - 要在 str 字符串中搜索的 substring。
  • start 和 end (可選) - 搜索 substring 的范圍 str[start:end]。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM