正則開始符 和結束符 巨坑

2021-08-21 20:24:15 字數 509 閱讀 6241

如下**,想要匹配出jar包name和version

file = "spring-test-4.3.9.release.jar"

matchobject = re.match(r'(.*)-(^\d.*\d$)\..*\.jar', file)

怎麼調都是錯的,非常頭痛。

正確寫法:

file = "spring-test-4.3.9.release.jar"

matchobject = re.match(r'(.*)-(\d.*\d)\..*\.jar', file)

原因分析:

^和$用於匹配原字元file的開頭和結尾,並不是匹配圓括號中的字元的開頭和結尾,對^和$的作用域的理解偏差,導致了這次問題。

結論:

^和$只能出現在pattern的最開頭和最結尾,不可出現在中間的任何位置。

檔案結束符

初學c 一道例題 使用者輸入一組數,在輸出資訊中顯示共輸入多少個負數。include using namespace std int main cout 當使用istream物件作為條件,結果是測試流的狀態。如果流是有效的,也就是說,如果讀入下乙個輸入是可能的,那麼測試成功。遇到檔案結束符或遇到無效...

C 檔案結束符

問題描述 windows下 int main string str while cin str cout n end 控制台輸入多個字串後,輸入檔案結束符crtl z,並未使while迴圈結束,該如何跳出while迴圈?解決方法 方法1 回車 ctrl z 回車方法2 改 設定自己的檔案結束符 st...

檔案結束符 End Of File

先看下面的程式 include includeusing namespace std int main ofstream outfile test.txt outfile abcdefg outfile.close char c ifstream infile test.txt while infi...