Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8720093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:57:13+00:00 2026-06-13T06:57:13+00:00

I have a String (filename): s_113_2.3gp How can I extract the number that appears

  • 0

I have a String (filename): s_113_2.3gp

How can I extract the number that appears after the second underscore? In this case it’s ‘2‘ but in some cases that can be a few digits number.

Also the number of digits that appears after the first underscore can vary so the length of this String is not constant.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:57:14+00:00Added an answer on June 13, 2026 at 6:57 am

    You can use a capturing group:

    preg_match('/_(\d+)\.\w+$/', $str, $matches);
    $number = $matches[1];
    

    \d+ represents 1 or more digits. The parentheses around that capture it, so you can later retrieve it with $matches[1]. The . needs to be escaped, because otherwise it would match any character but line breaks. \w+ matches 1 or more word characters (digits, letters, underscores). And finally the $ represents the end of the string and “anchors” the regular expression (otherwise you would get problems with strings containing multiple .).

    This also allows for arbitrary file extensions.

    As Ωmega pointed out below there is another possibility, that does not use a capturing group. With the concept of lookarounds, you can avoid matching _ at the start and the \.\w+$ at the end:

    preg_match('/(?<=_)\d+(?=\.\w+$)/', $str, $matches);
    $number = $matches[0];
    

    However, I would recommend profiling, before applying this rather small optimization. But it is something to keep in mind (or rather, to read up on!).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have string that look like Array that fetched from other webservice like this
I have a function that takes in a filename (string) and I want to
I have this ChucK code: examples/vento.txt => string filename; FileIO fio; // open a
I have this code: public static Account[] LoadXml(string fileName) { Account ths = new
HI, I have a string that looks like /dir/dir1/filename.txt I want to replace the
I have an interface that looks like this... public interface ITempFileNameBuilder { string DirectoryPath
I have a string literal as follows: string filename = @C:\myfolder\myfile.jpg; When I use
I have the below code: public void OpenFile(string FileName) { if (FileName == null)
I have the following domain: public class FileInformation { public String FileName; public String
I have a simple file read and write function. private void WriteToFile(String filename, String

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.