简体   繁体   中英

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

/\\(\d+)$/

See the regex demo

The \\\\(\\d+)$ regex matches:

  • \\\\ - a literal \\ symbol
  • (\\d+) - Group 1: one or more digits
  • $ - 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 :

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM