3

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 Strings 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?)

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
AJMansfield
  • 4,039
  • 3
  • 29
  • 50
  • So what's the problem with converting to string and use regex? Doesn't it solve the problem? Other ways I can think of is using nested loops. – gerrytan Jan 16 '14 at 21:58
  • Converting to string is inefficient and potentially trickable (for example an array of strings might have your separator character inside some of the strings) – Tim B Jan 16 '14 at 21:59
  • 1
    There has been some similar previous discussion: http://stackoverflow.com/questions/1387027/java-regex-on-byte-array – Tim B Jan 16 '14 at 22:00

1 Answers1

0

The Matcher class will take a CharSequence as input. If your integers all have values of two bytes or less then you can wrap your int array in a CharSequence. Just mask off the high bits in the charAt() method.

Otherwise, you'll have to dig into the source code of Pattern and Matcher. This isn't going to be a trivial undertaking.

ccleve
  • 15,239
  • 27
  • 91
  • 157