1

Is there a way of printing out every character that satifies a given regular expression?

For example, can I print all characters that matches regular expression, let's say, in Javascript:

[A-Za-z_-]|[\u00C0-\u00D6]|[\u00D8-\u00F6]|[\u00F8-\u02FF]|[\u0370-\u037D]|[\u037F-\u1FFF]|[\u200C-\u200D]|[\u2070-\u218F]|[\u2C00-\u2FEF]|[\u3001-\uD7FF]|[\uF900-\uFDCF]|[\uFDF0-\uFFFD]|[\u10000-\uEFFFF]

Example taken from Turtle specification.

EDIT: JavaScript implementation of the solution proposed by Toby and Peter Boughton.

var out = "",
  str = "";
for (var i = 32; i < 983040; i++) {
  str = String.fromCharCode(i);
  if (str.match(/[A-Za-z_-]|[\u00C0-\u00D6]|[\u00D8-\u00F6]|[\u00F8-\u02FF]|[\u0370-\u037D]|[\u037F-\u1FFF]|[\u200C-\u200D]|[\u2070-\u218F]|[\u2C00-\u2FEF]|[\u3001-\uD7FF]|[\uF900-\uFDCF]|[\uFDF0-\uFFFD]|[\u10000-\uEFFFF]/)) {
    out += str;
  }
}
console.log(out);
Jindřich Mynarz
  • 1,563
  • 1
  • 16
  • 31
  • This has been asked several times before on SO - only able to supply one link in the duplicate field, but here's a few more: http://stackoverflow.com/questions/2905229/reverse-regular-expressions-to-generate-data http://stackoverflow.com/questions/205411/random-string-that-matches-a-regexp http://stackoverflow.com/questions/748253/how-to-generate-random-strings-that-match-a-given-regexp http://stackoverflow.com/questions/1015449/regex-like-syntax-or-cfg-for-generating-cartesian-product-of-concatenated-string – Peter Boughton Aug 16 '10 at 16:13
  • You're right that the core of these questions is similar, but in the links you've provided the problem is to generate a random string that matches the given regular expression. I just want to get every possible *character* that matches the expression. – Jindřich Mynarz Aug 16 '10 at 16:16
  • Hmmm, didn't see that it was a one-character regex. Kinda odd that, but Toby's suggestion will work. – Peter Boughton Aug 16 '10 at 16:21

1 Answers1

0

I think the only way to do what you ask would be to loop through all possible characters one-by-one and "collect" each one that's a match into a buffer of some sort.

Toby
  • 7,354
  • 3
  • 25
  • 26
  • Well, but then there's the question how can you generate all possible characters? – Jindřich Mynarz Aug 16 '10 at 16:17
  • 1
    Something like `for (i=32;i<983040;i++){ if (chr(i).matches(regex){ output(chr(i)) } }` - that's not working code, but should give you the idea. Implementing it not as a regex would be faster. – Peter Boughton Aug 16 '10 at 16:23