Hot answers tagged regular-expressions
14
It's easy to search if you break it down:
Regex Meaning Mathematica command
-------------------------------------------------
\w word character WordCharacter
{2,3} repeat 2 to 3 times Repeated[..., {2, 3}]
Combine it and use as:
StringMatchQ[{"a", "ab", "abc", "abcd"}, Repeated[WordCharacter, {2, 3}]]
(* {False, True, True, ...
11
What about this?
StringSplit[string, i : NumberString :> i]
Ok, everyone's giving answers that actually work with the $, so here's an edit, as @kguler and @MrWizard suggested
StringSplit[string, i : ("" | "$" ~~ NumberString) :> i] // StringTrim
10
Short Version: Use ".*\".*" to match an embedded quote, ".*\\\\.*" to match an embedded backslash.
This question deals with two distinct syntaxes -- Mathematica string syntax and regular expression syntax. Both syntaxes use \ as an escape character, so we'll need separate the two levels to see what is happening.
First, let's deal with the Mathematica ...
7
Would this do the trick?
selectWords[chars_, min_, max_] :=
Module[{charsset = Union[chars], charstally = Tally[chars], baselist,
baselistchars, baselistpicks},
baselist =
ToLowerCase[DictionaryLookup[x__ /; min <= StringLength[x] <= max]]];
baselistchars =
Select[Characters /@ baselist, Complement[#, charsset] === {} &];
...
6
Note that Rojo's solution splits the expression containing the dollar sign as well:
StringSplit["there are 1234 words and numbers 5678 in here $999", i : NumberString :> i]
{"there are ", "1234", " words and numbers ", "5678", " in here $", "999"}
If you don't want that splitting to happen, here's one way, using a regex:
StringSplit["there are 1234 ...
5
My attempt:
First we define the existing row, using dots to represent empty squares, and our hand of 7 letters.
row="...t.t...r..e..";
letters="aodalip";
Next define a function to count how many times each of our letters appears in a given string. Also run this function on our letters, to count how many of each we have.
...
3
Further variations:
using StringReplace:
List @@ StringTrim /@ StringReplace[string,
a : Except[{"$", DigitCharacter}] .. | NumberString | ("$" ~~ NumberString) :> {a}]
or, using the same replacement rule in StringCases:
StringTrim /@ StringCases[string,
a : Except[{"$", DigitCharacter}] .. | NumberString | ("$" ~~ NumberString) :> {a}]
...
3
I prefer the solutions given by Rojo and J.M. to the following one. But if you want to see a working version of your original approach with StringCases and RegularExpression, here is one possibility
StringCases[string, RegularExpression["([A-Za-z]|\\s)+|(\\$|\\d)+"]]
It returns
{"there are ", "1234", " words and numbers ", "5678", " in here ","$999"}
...
3
The following may not fully solve your problem, but seems to logically belong here and is too long for a comment.
I do use tabs in my code formatter. This question prompted me to write a palette for the formatter, which was long overdue (it surely can be improved). The palette should work with both "Input"-style and "Program"-style cells. The palette ...
2
I originally wrote this to help in guessing a word of known length from a bunch of letters, The word guessing game has 10 letters given so I tried to optimize this for speed. I reworked it here for scrabble:
string = "hkxefri";
words = DictionaryLookup[{Apply[Alternatives,
Characters[string]]} ..];
joined = StringJoin /@
...
2
I find great utility in setting up grids using Ctrl, which inserts a place holder at the cursor. The input is interpreted as a List and you can create additional rows using CtrlReturn. Here's a walk-through of your Grid example above:
Start with Grid[ and enter the first element in the list. Then press Ctrl,, which produces:
Next, enter the second ...
1
I am not sure if this qualifies for an answer but it is too long for a comment therefore I put it here. Feel free to delete. In the examples the string matches the regular expression, meaning the result is not empty. Looking at the output we get
StringCases["foo\"bar", RegularExpression[".*\".*"]] // InputForm
(* {"foo\"bar"} *)
StringCases["foo\"bar", ...
Only top voted, non community-wiki answers of a minimum length are eligible
