0

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
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
edcoder
  • 503
  • 1
  • 5
  • 19
  • Which language/regex engine? What do you mean by ‘*read*’? Capture? Replace? And you do know that forward-slash and back-slash are different characters, right? – Biffen Oct 12 '16 at 10:24
  • What is your sample data? – Nurjan Oct 12 '16 at 10:24
  • How do you think we can get to your data? – Arkej Oct 12 '16 at 10:24
  • 1
    @Arkej It's probably the path itself. – Biffen Oct 12 '16 at 10:25
  • sorry for confusion. My sample data is the string with the path. I want to read the numbers after the last slash. So my variable should just display the numbers. Hope I am clearer now. JavaScript. – edcoder Oct 12 '16 at 10:26
  • 3
    You may just use `/\\([0-9]+)$/` and grab Group 1 contents. – Wiktor Stribiżew Oct 12 '16 at 10:26
  • Is regular expression the only option for you(by requirement?) ? What language do you want/need to use? – kuskmen Oct 12 '16 at 10:26
  • @EricDavid This smells like an [XY problem](http://mywiki.wooledge.org/XyProblem). Do you simply want to get the filename part of a path? – Biffen Oct 12 '16 at 10:28
  • I am using LUA, so the best would be in LUA. However LUA uses more of a pattern matching style. I only need to get the filename @Biffen – edcoder Oct 12 '16 at 10:32
  • @EricDavid **1** [You should have told us about the *actual* problem from the start.](http://mywiki.wooledge.org/XyProblem) You should just use a function to get the filename. Forget about regex. **2** It's Lua now? Why did you say JavaScript before? – Biffen Oct 12 '16 at 10:33
  • @Biffen, my sincere apologies, my script editor allows to use javascript too. So trying with both ways. – edcoder Oct 12 '16 at 10:35
  • @EricDavid Please [edit] the question to describe the *actual* problem. And give it the proper tags. – Biffen Oct 12 '16 at 10:36
  • @Biffen, thanks. You are right. got the idea from there.. and used Wiktor's solution. Sorry for the confusion. – edcoder Oct 12 '16 at 10:47

2 Answers2

0

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+)$]]
    )
)
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Note that in case there are chars other than digits at the end, you can use a negated character class based regex like `/\\([^\\]+)$/`. The `[^\\]+` will match 1 or more symbols other than ``\``. – Wiktor Stribiżew Oct 12 '16 at 10:34
  • I added a Lua pattern solution. Basically, it is the same, just `\d` must be changed into `%d` Lua pattern, and `string.match` returns the list of captured values if they are defined in the pattern. Since there is 1 capture group, the result is the digits at the end. – Wiktor Stribiżew Oct 12 '16 at 10:40
0

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
roberrrt-s
  • 7,914
  • 2
  • 46
  • 57