We all know how we can use regular expressions to find things in strings. However, regular expressions are not, at least mathematically, limited to just actual String
s and char[]
s; one could use a regular expression to match a subsequence in an int[]
. However, I can't seem to find anything on using a regex with a non-character strinr. Does anyone else know of something like this?
This was inspired mainly by the huge number of 'array subsequence' type problems seen on sites like CodingBat. Usually, I would convert the array to a string just so I could use regexes on it, for instance my solution to Java > Warmup-2 > noTriples was
return Arrays.toString(nums).split("(\\d*+), \\1, \\1(?!\\d)").length == 1;
(I like golfing).
I considered doing something like
return new String(nums,0,nums.length).split("(.)\\1\\1").length == 1;
(which does work), but one can't use literal values (for instance, the compiler will reject any program with \u0000
-\u001F
in it, except for a couple), plus there are other issues.
Does anyone know of a way to do pattern matching against integer arrays directly? (either with some sort of regular expression, or even with a more restricted language?)