Using DeleteCases to match a string pattern
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I am trying to remove all of the elements from a list that contain the characters "err"
in that order.
I expect the following code to return an empty list:
DeleteCases["Aerr", __ ~~ "err"]
However it fails to recognize the pattern and will return the full List. What am I missing in the documentation for patterns? Perhaps it is an issue with DeleteCases
, as I can use StringMatchQ
to identify that the patterns match.
StringMatchQ["Aerr", __ ~~ "err"]
Returns True
.
pattern-matching filtering
New contributor
add a comment |Â
up vote
2
down vote
favorite
I am trying to remove all of the elements from a list that contain the characters "err"
in that order.
I expect the following code to return an empty list:
DeleteCases["Aerr", __ ~~ "err"]
However it fails to recognize the pattern and will return the full List. What am I missing in the documentation for patterns? Perhaps it is an issue with DeleteCases
, as I can use StringMatchQ
to identify that the patterns match.
StringMatchQ["Aerr", __ ~~ "err"]
Returns True
.
pattern-matching filtering
New contributor
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am trying to remove all of the elements from a list that contain the characters "err"
in that order.
I expect the following code to return an empty list:
DeleteCases["Aerr", __ ~~ "err"]
However it fails to recognize the pattern and will return the full List. What am I missing in the documentation for patterns? Perhaps it is an issue with DeleteCases
, as I can use StringMatchQ
to identify that the patterns match.
StringMatchQ["Aerr", __ ~~ "err"]
Returns True
.
pattern-matching filtering
New contributor
I am trying to remove all of the elements from a list that contain the characters "err"
in that order.
I expect the following code to return an empty list:
DeleteCases["Aerr", __ ~~ "err"]
However it fails to recognize the pattern and will return the full List. What am I missing in the documentation for patterns? Perhaps it is an issue with DeleteCases
, as I can use StringMatchQ
to identify that the patterns match.
StringMatchQ["Aerr", __ ~~ "err"]
Returns True
.
pattern-matching filtering
pattern-matching filtering
New contributor
New contributor
edited 14 mins ago
Carl Woll
63.2k282163
63.2k282163
New contributor
asked 29 mins ago
Karl
113
113
New contributor
New contributor
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
First note the FullForm
of your pattern:
__ ~~ "err" //FullForm
StringExpression[BlankSequence,"err"]
The pattern is a StringExpression
, so you must use a string function, e.g., StringMatchQ
instead of DeleteCases
, which is expecting a normal pattern. Here are some other possibilities:
list = "Aerr";
Select[list, Not @* StringMatchQ[__ ~~ "err"]]
Pick[list, StringMatchQ[list, __ ~~ "err"], False]
add a comment |Â
up vote
1
down vote
DeleteCases["Aerr", _?(StringMatchQ[ "*err"]) ]
For versions prior to version 10:
DeleteCases["Aerr", _?(StringMatchQ[#, "*err"] &)]
While I appreciate the working example, I would have loved further explanation of how it works. I've gathered that the "?" syntax is a pattern test, but how does the "_" pattern which is for a single character match "Aerr"? Also, is it a bug that the code in my original question didn't work, or a misunderstanding of how to use patterns?
â Karl
9 mins ago
@Karl, please see Carl' Woll's answer for a detailed explanation.
â kglr
5 mins ago
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
First note the FullForm
of your pattern:
__ ~~ "err" //FullForm
StringExpression[BlankSequence,"err"]
The pattern is a StringExpression
, so you must use a string function, e.g., StringMatchQ
instead of DeleteCases
, which is expecting a normal pattern. Here are some other possibilities:
list = "Aerr";
Select[list, Not @* StringMatchQ[__ ~~ "err"]]
Pick[list, StringMatchQ[list, __ ~~ "err"], False]
add a comment |Â
up vote
2
down vote
First note the FullForm
of your pattern:
__ ~~ "err" //FullForm
StringExpression[BlankSequence,"err"]
The pattern is a StringExpression
, so you must use a string function, e.g., StringMatchQ
instead of DeleteCases
, which is expecting a normal pattern. Here are some other possibilities:
list = "Aerr";
Select[list, Not @* StringMatchQ[__ ~~ "err"]]
Pick[list, StringMatchQ[list, __ ~~ "err"], False]
add a comment |Â
up vote
2
down vote
up vote
2
down vote
First note the FullForm
of your pattern:
__ ~~ "err" //FullForm
StringExpression[BlankSequence,"err"]
The pattern is a StringExpression
, so you must use a string function, e.g., StringMatchQ
instead of DeleteCases
, which is expecting a normal pattern. Here are some other possibilities:
list = "Aerr";
Select[list, Not @* StringMatchQ[__ ~~ "err"]]
Pick[list, StringMatchQ[list, __ ~~ "err"], False]
First note the FullForm
of your pattern:
__ ~~ "err" //FullForm
StringExpression[BlankSequence,"err"]
The pattern is a StringExpression
, so you must use a string function, e.g., StringMatchQ
instead of DeleteCases
, which is expecting a normal pattern. Here are some other possibilities:
list = "Aerr";
Select[list, Not @* StringMatchQ[__ ~~ "err"]]
Pick[list, StringMatchQ[list, __ ~~ "err"], False]
answered 10 mins ago
Carl Woll
63.2k282163
63.2k282163
add a comment |Â
add a comment |Â
up vote
1
down vote
DeleteCases["Aerr", _?(StringMatchQ[ "*err"]) ]
For versions prior to version 10:
DeleteCases["Aerr", _?(StringMatchQ[#, "*err"] &)]
While I appreciate the working example, I would have loved further explanation of how it works. I've gathered that the "?" syntax is a pattern test, but how does the "_" pattern which is for a single character match "Aerr"? Also, is it a bug that the code in my original question didn't work, or a misunderstanding of how to use patterns?
â Karl
9 mins ago
@Karl, please see Carl' Woll's answer for a detailed explanation.
â kglr
5 mins ago
add a comment |Â
up vote
1
down vote
DeleteCases["Aerr", _?(StringMatchQ[ "*err"]) ]
For versions prior to version 10:
DeleteCases["Aerr", _?(StringMatchQ[#, "*err"] &)]
While I appreciate the working example, I would have loved further explanation of how it works. I've gathered that the "?" syntax is a pattern test, but how does the "_" pattern which is for a single character match "Aerr"? Also, is it a bug that the code in my original question didn't work, or a misunderstanding of how to use patterns?
â Karl
9 mins ago
@Karl, please see Carl' Woll's answer for a detailed explanation.
â kglr
5 mins ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
DeleteCases["Aerr", _?(StringMatchQ[ "*err"]) ]
For versions prior to version 10:
DeleteCases["Aerr", _?(StringMatchQ[#, "*err"] &)]
DeleteCases["Aerr", _?(StringMatchQ[ "*err"]) ]
For versions prior to version 10:
DeleteCases["Aerr", _?(StringMatchQ[#, "*err"] &)]
edited 12 mins ago
answered 25 mins ago
kglr
168k8192395
168k8192395
While I appreciate the working example, I would have loved further explanation of how it works. I've gathered that the "?" syntax is a pattern test, but how does the "_" pattern which is for a single character match "Aerr"? Also, is it a bug that the code in my original question didn't work, or a misunderstanding of how to use patterns?
â Karl
9 mins ago
@Karl, please see Carl' Woll's answer for a detailed explanation.
â kglr
5 mins ago
add a comment |Â
While I appreciate the working example, I would have loved further explanation of how it works. I've gathered that the "?" syntax is a pattern test, but how does the "_" pattern which is for a single character match "Aerr"? Also, is it a bug that the code in my original question didn't work, or a misunderstanding of how to use patterns?
â Karl
9 mins ago
@Karl, please see Carl' Woll's answer for a detailed explanation.
â kglr
5 mins ago
While I appreciate the working example, I would have loved further explanation of how it works. I've gathered that the "?" syntax is a pattern test, but how does the "_" pattern which is for a single character match "Aerr"? Also, is it a bug that the code in my original question didn't work, or a misunderstanding of how to use patterns?
â Karl
9 mins ago
While I appreciate the working example, I would have loved further explanation of how it works. I've gathered that the "?" syntax is a pattern test, but how does the "_" pattern which is for a single character match "Aerr"? Also, is it a bug that the code in my original question didn't work, or a misunderstanding of how to use patterns?
â Karl
9 mins ago
@Karl, please see Carl' Woll's answer for a detailed explanation.
â kglr
5 mins ago
@Karl, please see Carl' Woll's answer for a detailed explanation.
â kglr
5 mins ago
add a comment |Â
Karl is a new contributor. Be nice, and check out our Code of Conduct.
Karl is a new contributor. Be nice, and check out our Code of Conduct.
Karl is a new contributor. Be nice, and check out our Code of Conduct.
Karl is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f184973%2fusing-deletecases-to-match-a-string-pattern%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password