簡體   English   中英

正則表達式只讀取最后一個斜杠后的數字

[英]Regular expression to read only digits after last slash

我正在嘗試使用正則表達式讀取最后一個 \\ 之后的數字,但無法讀取。

我試過使用[^\\\\]*$ , ([^/]+$)但兩者都不起作用。 第一個刪除數字。

請問你能指教嗎?

我的樣本數據是:

C:\Users\Documents\Projects\Austraila\Customer\Organisation\176276

在 JS 中,你可以使用

/\\(\d+)$/

查看正則表達式演示

\\\\(\\d+)$正則表達式匹配:

  • \\\\ - 文字\\符號
  • (\\d+) - 第 1 組:一位或多位數字
  • $ - 字符串的結尾。

 var path = "C:\\\\Users\\\\Documents\\\\Projects\\\\Austraila\\\\Customer\\\\Organisation\\\\176276"; var m = path.match(/\\\\(\\d+)$/); if (m) { console.log(m[1]); }

Lua解決方案

print(
    string.match(
        [[C:\Users\Documents\Projects\Austraila\Customer\Organisation\176276]], 
        [[\(%d+)$]]
    )
)

如果且僅當您一直使用這種結構時,您還可以使用.split() (OP 在標簽中提到了 JavaScript,所以我提供了這個替代方案作為答案)。

var url = "C:\Users\Documents\Projects\Austraila\Customer\Organisation\176276";
var split = url.split("\"); /* Divides the string into an array which is splitted by \ */
var item = url[url.length - 1] /* Grab the last value in the array (176276) */

console.log(item)
// Prints 176276

暫無
暫無

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

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