# Use of StringExpression as argument

Is it possible to use a StringExpression as a pattern for an argument? I.E. something like

foo[chars__~~ WordCharacter.. ~~ num:NumberString] := {chars, num};


At the moment, I'm using a somewhat more convoluted function like

bar[str_String /; StringMatchQ[str, chars__~~ WordCharacter.. ~~ num:NumberString]] :=
StringCases[str,
chars__~~ WordCharacter.. ~~ num:NumberString :> {chars, num}][[1]];

• Would you describe your use case a bit? My present answer assumes that you want the entire definition to not match if the string pattern does not match. It is appropriate if you are blending string and non-string arguments. However, if you are only operating on strings there may be another syntax that you find more convenient. – Mr.Wizard Sep 11 '13 at 15:08
• @Mr.Wizard The case I'm working with at the moment involves converting a string that contains a specific character sequence into a list of two strings, each of which contains the 2 halves of the string with one being the reverse of the other. In my case, it is simple to solve using a StringSplit and a rule, but I plan on using a fair bit of string manipulation (Splitting things into different chunks and reordering/manipulating them in different ways), and was hoping there was a very simple way to accomplish things like this. – Daniel Sep 11 '13 at 16:19
• If you could give an example of your more complex manipulation, preferably in a new question, I'd be happy to try my hand at making it more convenient. – Mr.Wizard Sep 12 '13 at 1:40

You can however eliminate the double pattern matching by using the shared-body syntax of With and Condition I described in the middle of my answer to Using a PatternTest versus a Condition for pattern matching:
foo[s_String] :=

where rule is your pattern and replacement rule. Note that if the pattern is not matched and StringCases returns {} Mathematica will act as if the entire definition did not match, apart from any side-effects induced during the trial evaluation.