0
$\begingroup$

I'm trying to extract a specific part of a long string, where the part of interest starts with "testx" and ends with a number before an underscore follows, so e.g., "abcd_xt_testx1_" or "abtt_xp_testx20_" and a successful extraction would look like "testx1" and testx20 respectively.

I have tried both using StringSplit with "_" as spliter, and alternatively also used StringTake but since I don't know whether it's a 1 digit or 2 digit number that follows "testx" I don't lnow how to enforce the extraction. For instance, if test = "abcd_xt_testx1_" then StringTake[test, StringPosition[test,"testx"][[1]]] would yield only "testx" without the number after it (here 1).

  • How can I correctly extract such substrings without a priori knowing how many digits the number that is attached to "testx" contains?
$\endgroup$
1
  • $\begingroup$ @march oh surprisingly complicated and neat! Thanks. Would you kindly simply copy paste it as an answer so I can accept? $\endgroup$
    – user52181
    Apr 18, 2019 at 15:26

1 Answer 1

3
$\begingroup$
StringCases[stringExpression, "testx" ~~ Longest[DigitCharacter ..]]
$\endgroup$

Your Answer

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