简体   繁体   English

正则表达式只读取最后一个斜杠后的数字

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

I'm trying to read the numbers after the last \\ using regular expressions but unable to do it.我正在尝试使用正则表达式读取最后一个 \\ 之后的数字,但无法读取。

I've tried using [^\\\\]*$ , ([^/]+$) but both don't work.我试过使用[^\\\\]*$ , ([^/]+$)但两者都不起作用。 The first one removes the numbers.第一个删除数字。

Please can you advice?请问你能指教吗?

My sample data is:我的样本数据是:

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

In JS, you may use在 JS 中,你可以使用

/\\(\d+)$/

See the regex demo查看正则表达式演示

The \\\\(\\d+)$ regex matches: \\\\(\\d+)$正则表达式匹配:

  • \\\\ - a literal \\ symbol \\\\ - 文字\\符号
  • (\\d+) - Group 1: one or more digits (\\d+) - 第 1 组:一位或多位数字
  • $ - end of string. $ - 字符串的结尾。

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

Lua solution : Lua解决方案

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

If, and only if, you're having this structure all the time, you can also use the .split() (OP mentioned JavaScript in the tags, so i'm providing this alternative as an answer).如果且仅当您一直使用这种结构时,您还可以使用.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