6
$\begingroup$

I have a list of strings:

lis:= {{"123 abc def 456"}, {"ghi jkl 789"}, {"012 mno pqr"}}

and I would like to remove digit characters from the beginning of each string (that has them) to give:

res:={{abc def 456"}, {"ghi jkl 789"}, {"mno pqr"}}

Thanks for suggestions!

$\endgroup$
6
  • $\begingroup$ Thanks Nasser and lericr! $\endgroup$
    – Suite401
    Aug 22, 2023 at 17:44
  • 2
    $\begingroup$ Do you require as output {{"abc def 456"}, {"ghi jkl 789"}, {"mno pqr"}} or (as interpreted by @Nasser, and your accepted answer) {{"abc", "def", "456"}, {"ghi jkl 789"}, {"mno", "pqr"}}? $\endgroup$
    – user1066
    Aug 22, 2023 at 18:50
  • $\begingroup$ Yeah, @user1066, now I'm confused. $\endgroup$
    – lericr
    Aug 22, 2023 at 19:37
  • $\begingroup$ @user1066 added second version if string should not have comments in it. Now it matches the output. $\endgroup$
    – Nasser
    Aug 22, 2023 at 20:11
  • $\begingroup$ Possible not useful information @Suite401 as you have a high reputation... but SetDelayed (:=) is usually not the right option for assigning objects like this mathematica.stackexchange.com/questions/8829/… $\endgroup$ Aug 23, 2023 at 12:26

6 Answers 6

6
$\begingroup$
StringDelete[#, RegularExpression["^[\\d\\s]+"]]&/@lis

(* {{"abc def 456"}, {"ghi jkl 789"}, {"mno pqr"}} *)

(Delete from the beginning of each string one or more digit characters (\\d) or whitespace characters (\\s), in any order)

$\endgroup$
1
  • 1
    $\begingroup$ Nice refinement. The OP was silent about whitespace, so I made a guess, but I like your guess better. $\endgroup$
    – lericr
    Aug 22, 2023 at 19:39
8
$\begingroup$
StringTrim[#, StartOfString ~~ DigitCharacter ... ~~ WhitespaceCharacter ...] & /@ lis

or

StringTrim[#, RegularExpression["^\\d*\\s*"]] & /@ lis
$\endgroup$
5
$\begingroup$

Using StringReplace:

lis = {{"123 abc def 456"}, {"ghi jkl 789"}, {"012 mno pqr"}, {"a 232"}, {"123abc"}, {"567    abc 12"}}

StringReplace[
    StartOfString ~~ DigitCharacter .. ~~ 
      Repeated[WhitespaceCharacter, {0, ∞}] ~~ x__ ~~ 
      EndOfString :> x][#] & /@ lis

You can choose to keep or replace leading/trailing spaces using the Repeatedcommand as desired.


Using StringDelete: (as requested in the title)

StringDelete[
    StartOfString ~~ DigitCharacter .. ~~ 
     Repeated[WhitespaceCharacter, {0, ∞}]][#] & /@ lis

{{"abc def 456"}, {"ghi jkl 789"}, {"mno pqr"}, {"a 232"}, {"abc"},
{"abc 12"}}

$\endgroup$
1
  • 1
    $\begingroup$ (+1) Nice, @Syed! :-) $\endgroup$ Aug 22, 2023 at 18:06
4
$\begingroup$

Another method to do this is as follows:

 lis = {{"123 abc def 456"}, {"ghi jkl 789"}, {"012 mno pqr"}, {"a 232"}};

 SetAttributes[f, Listable];
 f[str_] := StringJoin[
    Apply[
            Function[
                Take[Characters @ #,
                    Span[
                        
       Function[Det[Position[#, FirstCase[#, _ ? LetterQ]]]][
        Characters @ #],
                        All
                    ]
                ]
            ],
            {str}
        ]
   ];

enter image description here

@Syed suggests an interesting approach to achieve the same thing, which is as follows:

chars = (StringPosition[_?LetterQ][#] & /@ lis // #[[All, -1, 1]] &) /. {a_, a_} :> {a, -1};
MapThread[StringTake[#1, #2] &, {lis, chars}] (*Thanks, Syed!*)
$\endgroup$
1
  • 1
    $\begingroup$ Try: chars = (StringPosition[_?LetterQ][#] & /@ lis // #[[All, -1, 1]] &) /. {a_, a_} :> {a, -1} and MapThread[StringTake[#1, #2] &, {lis, chars}] $\endgroup$
    – Syed
    Aug 22, 2023 at 19:36
3
$\begingroup$

Using NumberString:

NumberStringQ[x_String] := StringMatchQ[x, NumberString]

join[x_] := {StringJoin @ StringRiffle[{x}]}

skip[{_?NumberStringQ, x__}] := join[{x}]
skip[x_] := join[x]

skip @* First @* StringSplit /@ lis

{{"abc def 456"}, {"ghi jkl 789"}, {"mno pqr"}}

$\endgroup$
3
  • $\begingroup$ Is NumberStringQ a newly introduced function. I don't have it in v12.2. Or can include the required code for it? $\endgroup$
    – Syed
    Aug 23, 2023 at 10:18
  • $\begingroup$ Sorry - I added it $\endgroup$
    – eldo
    Aug 23, 2023 at 10:24
  • $\begingroup$ Please add a brace at the end of join[x_] or remove the one at the front? $\endgroup$
    – Syed
    Aug 23, 2023 at 10:52
3
$\begingroup$

One possible way out of many

I saw the comments above. Here is a version to get the output as string and not list of strings if needed, change it to

lis={{"123 abc def 456"},{"ghi jkl 789"},{"012 mno pqr"},{"232 test"},{"1 a"}};
f[s_List]:=Module[{m,s0=First@s},
    m=StringSplit[s0];
    If[Head[ToExpression[First@m]]===Integer,{StringDrop[s0,StringLength[First@m]+1]},s]
];

Map[f, lis]

Mathematica graphics

Old answer

lis={{"123 abc def 456"},{"ghi jkl 789"},{"012 mno pqr"},{"a 232"}}
f[s_List]:=Module[{m},
   m=StringSplit[s[[1]]];
   If[Head[ToExpression[First@m]]===Integer,Join[Rest[m]],s]
]
Map[f,lis]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ This doesn't match the expected output. Although, the expected output provided by the OP has a syntax error, so maybe I misinterpreted. $\endgroup$
    – lericr
    Aug 22, 2023 at 19:36

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.