3
$\begingroup$

The documentation has the following example to find all runs in a string containing only "a" and "b":

StringCases["aababbcccdbaa", ("a" | "b") ..]
{"aababb", "baa"}

I would like to find all runs in a string containing only numbers. E.g.,

findnumbers["DataSummary_1N_5M_10L.dat"]
{"1", "5", "10"}

What is the nice way to do this, besides just writing out explicitly in the second argument of StringCases ("1" | "2" | "3" | "4" |...etc... ) ..? Thanks!

$\endgroup$
4
  • 2
    $\begingroup$ check DigitCharacter or NumberString. $\endgroup$
    – Kuba
    Commented Aug 10, 2017 at 19:44
  • $\begingroup$ at least closely related: 8938 $\endgroup$
    – Kuba
    Commented Aug 10, 2017 at 19:46
  • $\begingroup$ Thanks! This is exactly what I was looking for. $\endgroup$
    – Diffycue
    Commented Aug 10, 2017 at 19:46
  • $\begingroup$ Also related: (73172) $\endgroup$
    – Mr.Wizard
    Commented Aug 11, 2017 at 2:14

1 Answer 1

5
$\begingroup$
data = "DataSummary_1N_5M_10L.dat";

StringCases[data, NumberString]

{"1", "5", "10"}

$\endgroup$
2
  • 3
    $\begingroup$ StringCases[str, DigitCharacter ..] should also work. $\endgroup$ Commented Aug 10, 2017 at 19:49
  • $\begingroup$ Indeed, it does ! Very elegant $\endgroup$
    – eldo
    Commented Aug 10, 2017 at 19:53

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.