1

I have a string like this

file-myfle_20130207_094852am.csv

how do i go about writing the regex to extract only the numbers

20130207094852
3
  • find -name '*.csv' | sed s/[^[:digit:]]// Commented Feb 7, 2013 at 4:27
  • What kind of tool are you using? What are you actually trying to do? (file renaming?) Commented Feb 7, 2013 at 4:37
  • Your question doesn't make a lot of sense. Regular expressions don't "extract" anything, they match patterns. If you want to transform your input string to the desired output string, that's a somewhat different thing to do. Commented Feb 7, 2013 at 4:55

2 Answers 2

5

Like this (this is pretty common among Regex tutorials)

\d+

Each group will be the numbers.

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

5 Comments

Might want to use * to account for the lack of numbers.
@AshwinMukhija, no. No number is not a number (which was of interest). Off topic, +5 for \d+, fu SO... ;-)
This is at best incomplete solution. The numbers in the question are separated by _.
@nhahtdh Without a known language to combine them back together.. how do you propose I update my answer?
@SimonWhitehead: That's why I just leave a plain comment. I am curious why no one even ask the OP to include more information.
0

See my comment on the question for context.

From a shell:

echo file-myfle_20130207_094852am.csv | tr -cd /0-9/
20130207094852

With Perl:

echo file-myfle_20130207_094852am.csv | perl -pe 's/\D//g'
20130207094852

Comments

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.