17

StringMatchQ["aabbc", "*a*c*"] True StringMatchQ["aabbc", "*b*a*"] False You can also use LongestCommonSequence to construct a function ClearAll[strngCntnsQ] strngCntnsQ = LongestCommonSequence[##] == #2 &; strngCntnsQ["aabbc", "ac"] True strngCntnsQ["aabbc", "ba"] False


15

You can use the fact that once an object is replaced, it doesn't get replaced by any further rules. So, just add a rule that replaces a DateObject with itself: expr = {1 -> a, 2 -> b, 3 -> c, 4 -> {5 -> d, 6 -> e, 7 -> Today}, 8 -> Yesterday, 9 -> {10 -> f, 11 -> {12 -> Tomorrow, 13 -> g}}}l expr /. {a_DateObject :> ...


14

Is this what you need? StringContainsQ["aabbc","a" ~~ ___ ~~ "c"] True The following documentation pages should help you get going with string patterns in Wolfram Language: https://reference.wolfram.com/language/tutorial/StringPatterns.html https://reference.wolfram.com/language/tutorial/WorkingWithStringPatternsOverview.html edit Here is a function ...


12

Most of the issue here is related to subtle evaluation and pattern-matching features in Haskell that have no analog in Wolfram Language (WL). The ~ operator has no real equivalent within WL. Nevertheless, the issues can be approximated in WL and such approximation will indeed make judicious use of Hold attributes to induce lazy evaluation semantics. The ...


5

Try these: StringCases[dep, "((nsubj, (" ~~ w : WordCharacter .. ~~ ", " ~~ DigitCharacter .. ~~ "))" :> w] StringCases[dep, "(" ~~ w : WordCharacter .. ~~ ___ :> w] {{"He"}, {"I", "he"}} {{"wrote"}, {"read"}}


5

We can get the desired result using Replace in several ways: 1. We can temporarily change the behavior of Framed inside expressions with head DateObject (using Block and TagSetDelayed): Block[{Framed}, Framed /: DateObject[Framed[a_], b___] := DateObject[First @ a, b]; Replace[expr, a_List :> Framed[Column @ a], All]] Alternatively, Block[{Framed},...


4

StringContainsQ["aaabc", RegularExpression["a.*c"]] True


3

Wrap the patterns with parantheses: StringMatchQ[(StartOfString ~~ "a" ~~ _ ~~ _ ~~ _) | (StartOfString ~~ "b" ~~ _ ~~ _ ~~ _)]["abcd"] True Alternatively, use the input form of Alternatives: StringMatchQ[Alternatives[StartOfString ~~ "a" ~~ _ ~~ _ ~~ _, StartOfString ~~ "b" ~~ _ ~~ _ ~~ _]]["abcd"] True


3

ReplaceAll works top-down, so you can add a rule to your replacement to leave HoldForm objects alone. Assuming the outer Hold wrapper isn't HoldForm you could do: held = Hold[a + i + HoldForm[a + k] + l]; rule = {h_HoldForm :> h, HoldPattern[s_Symbol] :> RuleCondition[s]}; held /. rule Hold[b + i + HoldForm[a + k] + l] If the outer Hold wrapper ...


3

You can use a pure function to store the boolean expression: var = # == "a" &; You can use it with PatternTest Select[{"abc", "def"}, StringMatchQ[StartOfString ~~ c1_?var ~~ __ ~~ EndOfString]] {"abc"} or with Condition: Select[{"abc", "def"}, StringMatchQ[StartOfString ~~ c1_ ~~ __ ~~ EndOfString /; var[c1]]] {"abc"}


3

Maybe something like: ClearAll[cyclicPattern, cyclicPatternRule] cyclicPattern[n_, h_: X] := Times @@ (h /@ (Pattern[#, Blank[]] & /@ # & /@ Partition[Symbol["x" <> ToString[#]] & /@ Range[n], 2, 1, 1])) cyclicPatternRule[n_, h_: X] := cyclicPattern[n, h] -> Symbol[SymbolName[h] <> ToString[n]] cyclicPatternRule /@ Range[...


3

Another option is Developer`ReplaceAllUnheld. expr = {1 -> a, 2 -> b, 3 -> c, 4 -> {5 -> d, 6 -> e, 7 -> Today}, 8 -> Yesterday, 9 -> {10 -> f, 11 -> {12 -> Tomorrow, 13 -> g}}}; Block[{DateObject}, Attributes[DateObject] = HoldAll; Developer`ReplaceAllUnheld[expr, List -> Framed@*Column@*List] ] Also see ...


1

A Regex attempt: StringCases[#, RegularExpression["[(]nsubj,\s+[(]([^,]+)"] :> "$1"]&/@dep {{He}, {I, he}} StringCases[#, RegularExpression["^[(]([^,]+)"] :> "$1"]&/@dep {{wrote}, {read}}


1

I think perhaps you are seeking or misunderstanding the function of /; which is shorthand for Condition. You write: "how does mathematica know what x is when checking x==# is true or not." The value bound to the pattern x_ is substituted into the right hand side of /; where x appears, in the manner of rules or function definitions. Compare: f[x_] := ...


1

It is applying the test to each element of the list. Consider this: list = {1, 2} MemberQ[list, x_ /; (x + # == 4)] & /@ list {False, True} When x and # are both 2 the x_ pattern as 2 is tested in MemberQ. Otherwise the x_ pattern is False. I.e. MemberQ[list, False] which in turn yields False. In the case of your code MemberQ[list, x_ /; (x == #)...


1

It's useful to have a test input on hand when taking apart code like the one in the OP. ff = Function[t, Piecewise[{{t/4, t < 0}, {t/2, t < 3}, {3/2 + (t - 3)*3, True}}]]; First (with a slight change of variables to help clarify the operations), Reduce[x == ff[y], y, Reals] (x <= 0 && y == 4 x) || (0 < x <= 3/2 && y == 2 ...


Only top voted, non community-wiki answers of a minimum length are eligible