tags:

views:

38

answers:

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);
A: 

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
Well, but then there's the question how can you generate all possible characters?
jindrichm
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