Consider a string like:
str = "AabHHioPjggtYuggbwq";
I would like to have a function getpairs[x_String] that would extract for me:
getpairs[str]
{"Aa","HH","gg","gg"}
Any suggestions how to do this?
Consider a string like:
str = "AabHHioPjggtYuggbwq";
I would like to have a function getpairs[x_String] that would extract for me:
getpairs[str]
{"Aa","HH","gg","gg"}
Any suggestions how to do this?
str = "AabHHioPjggtYuggbwq";
StringCases[str, x_ ~~ x_, IgnoreCase -> True]
{"Aa", "HH", "gg", "gg"}
Or in the generalized version of the question:
str = "AaAbHHhhhioPjggtYuggGgGbwq";
StringCases[str, x_ ~~ (x_) .., IgnoreCase -> True]
{"AaA", "HHhhh", "gg", "ggGgG"}
x_ ~~ x_ ...
– Martin Ender
Feb 14 '17 at 16:43
x_. default pattern, but there should be no issue since that is (apparently) not valid in a string expression anyway.
– george2079
Feb 15 '17 at 15:32
getpairs[str_String] := StringJoin @@@ Select[
Partition[Characters[str], 2, 1],
SameQ @@ ToUpperCase[#] &]
str = "AabHHioPjggtYuggbwq";
getpairs[str]
(* {"Aa", "HH", "gg", "gg"} *)
"aAa" (or whether the OP assumes that this case will never appear in the input).
– Martin Ender
Feb 15 '17 at 14:29
I will give a RegularExpression version here.
str = "AabHHioPjggtYuggbwq";
StringCases[str, RegularExpression["(?i)(\\w)\\1"]]
{"Aa", "HH", "gg", "gg"}
Per this comment,that the string contain 3 or more letter case
str = "aAabHhHioPjggtYuggbwwq";
StringCases[str, RegularExpression["(?i)(\\w)\\1+"]]
{"aAa", "HhH", "gg", "gg", "ww"}
\\1+ instead of \\1*\\1 (and it would be marginally more efficient to switch that around to \\1\\1* if you kept it). Also note that \\w matches more than just letters (but not all characters either).
– Martin Ender
Feb 15 '17 at 13:28
aaa? If so do you expect one or twoaain the result? (Or alternatively, if there'saAa, do you want bothaAandAa?) – Martin Ender Feb 14 '17 at 16:24...,"aaa",...,"aAa",...,– Kagaratsch Feb 14 '17 at 16:41