# Regular expression too large problem

pattern = Names["*"];

StringMatchQ["Bold", pattern[[1 ;; -1 ;; 2]]]

(*
True
*)

StringMatchQ["Bold", pattern]


### Error Information:

RegularExpression::msg20: Regular expression too large in StartOfString~~<<5433>>}~~EndOfString. >>

question is how to overcome this problem, or is there some option to adjust this.

following is some extends for pattern, this is something different from the case in Martin's answer.

pattern=#~~__&/@Names["*"];


Context is in dealing with the String generated by Input Cells, then one is in front of the large number of Names and Pattern with Names. Since my question about context is not so clear, I can't add too much detail.

• Is the self-edit intended as a solution, clarification, or something else? – Mr.Wizard Feb 14 '15 at 17:04
• @Mr.Wizard hi, something clarification, why I donot do like the solution in Martin's answer – HyperGroups Feb 14 '15 at 17:07
• Is the use of Names merely a terse way to generate a long list of strings or does it have some bearing on your real application? If you are looking for a solution rather than merely an understanding of the origin of the problem I suggest you provide some context. – Mr.Wizard Feb 14 '15 at 17:13
• @Mr.Wizard yes, actually , both as your thoughts. Context is in dealing with the String generated by Input Cells, then one is in front of the large Names and Patterns with Names. Since my question about context is not so clear, I can't add too much detail. – HyperGroups Feb 14 '15 at 17:28
• I just have checked your code with version 10.4 and found that there is no error anymore: the output of StringMatchQ["Bold", pattern] is True without any messages. – Alexey Popkov Mar 30 '16 at 12:07

## Source

Mathematica makes use of the PCRE library. According to http://www.pcre.org/pcre.txt :

Within a compiled pattern, offset values are used to point from one part to another (for example, from an opening parenthesis to an alter- nation metacharacter). By default, in the 8-bit and 16-bit libraries, two-byte values are used for these offsets, leading to a maximum size for a compiled pattern of around 64K. This is sufficient to handle all but the most gigantic patterns. Nevertheless, some people do want to process truly enormous patterns, so it is possible to compile PCRE to use three-byte or four-byte offsets by adding a setting such as

 --with-link-size=3


to the configure command. The value given must be 2, 3, or 4. For the 16-bit library, a value of 3 is rounded up to 4. In these libraries, using longer offsets slows down the operation of PCRE because it has to load additional data when handling them. For the 32-bit library the value is always 4 and cannot be overridden; the value of --with-link- size is ignored.

Apparently you have run into this limitation:

StringPatternPatternConvert[Names["*"]][[1]] // StringLength

81958


I don't believe you can change this for Mathematica.

## Solutions

As Martin points out you do not even need string patterns for verbatim matches, but in other cases I suggest you split the pattern into smaller parts and run multiple passes.

StringCases apparently does not convert its second parameter to a single regular expression:

pattern = # ~~ __ & /@ Names["*"];

StringCases["Bold 123", pattern]

{"Bold 123"}


One could therefore use something like:

pattern = StartOfString ~~ # ~~ __ & /@ Names["*"];

StringCases["Bold 123", pattern] =!= {}


If you just want to check if Bold exists in Names, there's no need for string matching:

MemberQ[Names["*"], "Bold"]
(* True *)


or even

Names["Bold"] != {}
(* True *)


Names also takes more elaborate string patterns, just as StringMatchQ does.

That being said, even in your example, I don't understand, why you're matching all the names against Bold instead of the other way round:

Or @@ StringMatchQ[Names["*"], "Bold"]
(* True *)

• hi, how about pattern=#~~__&/@Names["*"];` – HyperGroups Feb 14 '15 at 15:05
• @HyperGroups If you really want to form one huge pattern from the the names, see Mr. Wizard's answer. But I don't see why you'd want to do that. – Martin Ender Feb 14 '15 at 16:30