5
$\begingroup$

I have the following list:

list1 = {"O                    21   B21      20   A20      19   D19      0", \
"90  O            -0.28487   1.23835", "91  O            -0.23761   \
1.29381", "92  O            -0.23093   1.19437", "93  O            \
-0.19657   1.53843", "94  O            -0.17235   1.46275", "O        \
            21   B21      20   A20      19   D19      0", "90  O      \
      -0.28601   1.23805", "91  O            -0.23863   1.29489", "92 \
 O            -0.23206   1.19334", "93  O            -0.19730   \
1.53926"}

I need only the elements that do not have the expression: B21, D19, etc.... Like that:

list1 = { \
"90  O            -0.28487   1.23835", "91  O            -0.23761   \
1.29381", "92  O            -0.23093   1.19437", "93  O            \
-0.19657   1.53843", "94  O            -0.17235   1.46275",  "90  O      \
      -0.28601   1.23805", "91  O            -0.23863   1.29489", "92 \
 O            -0.23206   1.19334", "93  O            -0.19730   \
1.53926"};

So I did:

Cases[%, Except[
  RegularExpression["[A-Z]" ~~ __Integer]], \[Infinity]].

But It did no work.

$\endgroup$
1
  • 1
    $\begingroup$ Please read: (6998) $\endgroup$
    – Mr.Wizard
    Oct 13, 2014 at 18:03

4 Answers 4

4
$\begingroup$
Pick[list1, StringFreeQ[#, RegularExpression["[A-Z]\\d"]] & /@ list1] 
Cases[list1, x_?(StringFreeQ[#, RegularExpression["[A-Z]\\d"]] &)]
Select[list1, StringFreeQ[#, RegularExpression["[A-Z]\\d"]] &]
DeleteCases[list1, x_?(!StringFreeQ[#, RegularExpression["[A-Z]\\d"]] &)]

and all of the above with RegularExpression["[A-Z]\\d"] replaced with

Alternatives @@ CharacterRange["A", "Z"] ~~ DigitCharacter

all give

Pick[list1, StringFreeQ[#, RegularExpression["[A-Z]\\d"]] & /@ list1] //Column

enter image description here

where

list1 //Column

enter image description here

$\endgroup$
2
  • $\begingroup$ I did not know th pick comand, is it new? $\endgroup$
    – locometro
    Oct 13, 2014 at 18:31
  • $\begingroup$ @user7096, Pick has been around since version 5; it is often more convenient/powerful than its older siblings Select and Cases. $\endgroup$
    – kglr
    Oct 13, 2014 at 18:52
5
$\begingroup$
Select[list1,  StringFreeQ[#, ___ ~~ LetterCharacter ~~ DigitCharacter .. ~~ ___] &]

(*
90  O            -0.28487   1.23835
91  O            -0.23761   1.29381
92  O            -0.23093   1.19437
93  O            -0.19657   1.53843
94  O            -0.17235   1.46275
90  O            -0.28601   1.23805
91  O            -0.23863   1.29489
92  O            -0.23206   1.19334
93  O            -0.19730   1.53926
*)
$\endgroup$
2
$\begingroup$

One can also use StringReplace

DeleteCases[StringReplace[list1, x___ ~~ LetterCharacter ~~ NumberString ~~ y___ :> ""], ""]
$\endgroup$
0
$\begingroup$

I am not sure about this but it looks like (for such specific list) that you can do it like this:

Select[list1, Xnor[StringMatchQ[#, "O" ~~ __]] &]
$\endgroup$

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.