0

I have about 500 lines stored in a text file. Each line look like Filename_662344.xlsx , 324.

I would like to count numbers at the end of each line (324 in this case).

My idea is to delete part of lines using preg_replace() function. I tried preg_replace("/[^0-9]/", '', $line); , but the result was 662344324

How should the pattern look like if I want to delete the filename (including numbers);

Thanks!

1
  • 1
    Just use $parts = explode(',', $line) and then use $part[1] ... no need to fiddle with regex here? Commented Dec 7, 2014 at 16:36

2 Answers 2

2

Can try this regex.

$line = 'Filename_662344.xlsx , 324';
$line = preg_replace("/^(.*?), /", '', $line);
echo $line;

Regex Demo

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

Comments

1

\d+$ will match the numbers at the end of a string

See Here for an example of it.

If you are trying to remove everything except the numbers, you can just use (^.*, ). This starts at the beginning of the line and select everything up to the comma. See Here for an example

1 Comment

Updated my answer if he wants to select everything except the number

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.