1
$\begingroup$

If I have some strings eg:

str1 = "12-26b-14a";
str2 = "12-21h-14";

and I want to replace the string with a value based on the following rules:

testru = {"12-26b" -> xx, "12-21h" -> yy, "12-42e" -> zz};

I can make the following criteria:

DeleteCases[
             ReplaceAll[If[StringMatchQ[str1, # ~~ __], #] & /@ 
              Keys@testru, testru],
            Null][[1]]

xx

DeleteCases[
             ReplaceAll[If[StringMatchQ[str2, # ~~ __], #] & /@ 
              Keys@testru, testru],
            Null][[1]]

yy

This works, but I suspect there are much simpler/ more elegant ways to perform such a seemingly simple task.

In particular, ReplaceAll feels like overkill for a single element!

Any suggestions for an improved method?

EDIT

I'd rather not mess with the structure of testru since I use this list for other purposes.

Also the strings do not have definite lengths so clipping the string by a certain number of characters is not viable.

$\endgroup$
  • $\begingroup$ "strings do not have definite lengths" - but if the prefixes are fixed, then Carl's use of StringStartsQ[] for matching suffices. $\endgroup$ – J. M. will be back soon Sep 28 '18 at 1:46
  • $\begingroup$ @J.M.issomewhatokay., Yes, it would be a good solution (+1 vote). I added the second 'edit' comment to head off other answers based on fixed length strings. $\endgroup$ – geordie Sep 28 '18 at 1:56
  • 1
    $\begingroup$ Into what do you want str1 and str2 to transform? $\endgroup$ – Αλέξανδρος Ζεγγ Sep 28 '18 at 4:29
1
$\begingroup$

Maybe something like:

Replace[
    {str1, str2},
    {_?(StringStartsQ["12-26b"])->xx, _?(StringStartsQ["12-21h"])->yy},
    {1}
]

{xx, yy}

Addendum to address OP edits

If you need to use testru as is, then you could do something like:

Replace[
    {str1, str2},
    Replace[testru, Rule[a_, b_] :> Rule[PatternTest[Blank[], StringStartsQ[a]], b], {1}],
    {1}
]

{xx, yy}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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