2

I used the following regular expression on the Taxonomy term name field to exclude strings consisting solely of alphabetical names:

[^a-zA-Z]

Now I want to exclude those strings plus any string with a space.

What I want to do:

  • Hello (no)
  • Hi how are you (no)
  • ハロー (ok)

I tried this:

[^a-zA-Z\s]

This excludes alphabetical strings but not alphabetical strings with a space:

  • Hello (no)
  • Hi how are you (yes-> should be no)
  • ハロー(ok)

What am I doing wrong?

1 Answer 1

3

The Views module uses MySQL regular expressions, and \s is not recognized in those regular expressions.

You should try with [^a-zA-Z[:blank:]], or [^a-zA-Z[:space:]]. The first excludes any whitespace character, while the second excludes any space, tab, newline, and carriage return.

In the views_handler_filter_string class, the method associated with the regular expression is the following one.

  function op_regex($field) {
    $this->query->add_where($this->options['group'], $field, $this->value, 'RLIKE');
  }
// Add regexp support for MySQL.
if (Database::getConnection()->databaseType() == 'mysql') {
  $operators += array(
    'regular_expression' => array(
      'title' => t('Regular expression'), 
      'short' => t('regex'), 
      'method' => 'op_regex', 
      'values' => 1,
    ),
  );
}
2
  • I know this answer is 7 years old now, but it might be good to note that MariaDB 10.0.5 switched to PCRE, so all the usual regex goodness such as shorthand character classes, look-ahead and look-behind, and option modifiers should be supported in Views if your site is running on MariaDB rather than MySQL. I've just tested it on one of my own sites and it seems to work great. I've got a filter set up which uses a regex with the case insensitive modifier and a negative look-ahead, and I get exactly the results I would expect, with no errors.
    – morbiD
    Commented Jun 12, 2019 at 16:19
  • Yes, I should add a note about those database engines that Database::getConnection()->databaseType() reports to be mysql.
    – apaderno
    Commented Oct 19, 2019 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.