1

As anyone who's seen my posts will know, I'm a regex pleb. Anyone can tell me how to get the ID from this file name, would be very grateful?

/data/64786/sasf.zip

I need the 64786 bit from this line, always follows /data/ - anyone able to help quickly?

0

4 Answers 4

2
~/data/(\d+)/~

This will match a sequence of decimals (0-9) immediately following the string /data/. Match is in group 1.

If the string can also look like /data/64786 (i.e., nothing after the number), use ~/data/(\d+)~ instead. Actually, you can use this either way since \d+ will be greedy per default and thus match as many decimals as possible. (Tested at Rubular.)

Sign up to request clarification or add additional context in comments.

2 Comments

Though all others seem to work this was the best answer for the job. Thanks!
@WiseDonkey You're welcome. When posting regexp questions you should take extra care to state both what's certain ("always follows /data/") and what varies (e.g., will there always be a slash after the ID?). This will reduce the amount of assumptions (and - not least - the following questioning of those) :)
1

This is a number. Use the \d+ placeholder for decimals:

preg_match("#(\d+)#", $str, $matches);
$id = $matches[1];

If the id always occurs at the same place, then you can add the slashes #/(\d+)/# for reliability. Most text strings can be used as-is in regular expressions as anchors.

[reference, tools] https://stackoverflow.com/questions/89718/is-there-anything-like-regexbuddy-in-the-open-source-world

Comments

1

You could grab the ID using the following code:

$filename = '/data/64786/sasf.zip';
$prefix = '/data/';
$prefix_pos = strpos($filename, $prefix);

if($prefix_pos !== false)
{
    $prefix_pos_end = $prefix_pos + strlen($prefix);
    $slash_pos   = strpos($filename, '/', $prefix_pos_end);
    $id          = substr($filename, $prefix_pos_end, $slash_pos - $prefix_pos_end);
}
else    $id         = false;

6 Comments

This might be faster than a regular expressionbut it will also not check the length of the ID (might be 5 or 500000 as well) and it won't check the beginning being "data".
@Mario: That is not correct. I am searching the ID using substring and beginning from position 6 (after '/data/'). Also, I am searching the slash after the ID. So if doesn't really matter if the number is 50 or 500000.
Yes, interpreted that part in the wrong way, however there's no guarantee it's indeed "/data/" in front of the number and the trailing slash can't be ommited.
@Mario At least there's no guarantee (from what can be read in the question) that the /data/ (six chars) is at the beginning of the string/line.
@Mario: That is true, but does your answer guarantee it is indeed "/data/"?
|
0

Why don't you try: basename(dirname("/data/64786/sasf.zip"))

1 Comment

This will break in case someone adds an additional "/" for whatever reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.