5
$\begingroup$

I want to find the word "circle" and "circles" in the list.

list = {"circle" , "picircle" , "circles" }

The regex code I wrote which isn't working:

StringCases[#, 
RegularExpression["(/\\b)(\\w*circle\\w*)(\\b//g)"]] & /@ list

The output I want:

{{circle},{},{circles}}

The output I get:

{{},{},{}}
$\endgroup$
7
$\begingroup$

The logic of you regular expression is not entirely clear to me.

Mathematica's RegularExpression does not need any delimiters and I don't think that there is a "global" switch. In any case, the following matches all single words "circle", "circles", "Circle" and "Circles":

list = {"circle", "picircle", "circles", "Circle"};
StringCases[#, RegularExpression["\\b[Cc]ircles?\\b"]] & /@ list
(* {{"circle"}, {}, {"circles"}, {"Circle"}} *)
$\endgroup$
2
  • $\begingroup$ how can I can change this so that it can be used for "Circles" as well? $\endgroup$ – Aryan Aug 10 '20 at 9:44
  • 1
    $\begingroup$ @Aryan I have updated the answer. $\endgroup$ – Natas Aug 10 '20 at 9:52
5
$\begingroup$
list = {"circle", "picircle", "circles"};
StringCases[#, 
   StartOfString ~~ "circle" | "circles" ~~ EndOfString] & /@ list
StringCases[#, RegularExpression["^(circle|circles)$"]] & /@ list
$\endgroup$
1
  • 1
    $\begingroup$ your solution works! i upvoted it, but I wanted it in RegEx, hence couldn't verify it as the answer :) $\endgroup$ – Aryan Aug 10 '20 at 9:39

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.