Support from Acquia helps fund testing for Drupal Acquia logo

Comments

timodwhit’s picture

Looking into it further, it looks like the reason the flags aren't allowed in the comparison is because

case 'regex':
     return (bool) preg_match('/'. str_replace('/', '\\/', $text2) .'/', $text)

instead of

case 'regex':
     return (bool) preg_match($text2, $text)

It make sense why this is the case. But is there a way to have the code look like:

case 'regex':
     return (bool) preg_match('/'. str_replace('/', '\\/', $text2) .'/'.$text3, $text);

Where $text3 is the select list of flags?

return array(
'g' => t('global'),
'i' => t('ignoreCase'),
'm' => t('multiline'),
'x' => t('extended'),
's' => t('dotall'),
timodwhit’s picture

Title: Flags Support for Regex Possibility » Text comparison: Flags Support for Regular Expression
mitchell’s picture

Version: 7.x-2.1 » 7.x-2.x-dev

Flag support for regex?? Huh?..

http://livedocs.adobe.com/flex/3/html/12_Using_Regular_Expressions_10.html
http://gskinner.com/RegExr/ : UI also has this with some nice help text.

Everything is possible! ..but what an unfortunate terminology.
I guess these could be checkbox options, similar to RegExr.

Follow-up once we add this here: #1658092: Action: Regular Expression

Bobcrum’s picture

This would be immensely helpful for states like California, whose tax codes are insane... I would use it for ignoring the case on City names that are used to calculate taxes. As it stands now, I need to write separate rules for EVERY case permutation on 24 counties.

zonesny’s picture

Issue summary: View changes

Hi. Is there any movement on this feature request (i.e. implementing regex flags)? The original post was made in June 2012, and this is a much needed enhancement.

The following issue posts are all related to the need for this feature (and this is only a sample):

zonesny’s picture

While this feature should be properly implemented via the plugin (and checkboxes as the original poster suggested), if you desperately need the flags, and don't mind having them applied to all of your Rules regex, you could use the following hack in rules/modules/data.eval.inc:

function rules_data_text_comparison($text, $text2, $op = 'contains') {
  switch ($op) {
    case 'contains':
      return strpos($text, $text2) !== FALSE;
    case 'starts':
      return strpos($text, $text2) === 0;
    case 'ends':
     return strrpos($text, $text2) === (strlen($text) - strlen($text2));
    case 'regex':
//     return (bool) preg_match('/'. str_replace('/', '\\/', $text2) .'/', $text);
$matches = array();
     return (bool) preg_match_all('/'. str_replace('/', '\\/', $text2) .'/mi', $text, $matches);
  }
}

NOTE: the global (g) modifier does not exist, instead preg_match_all must be used, and a third parameter array must be passed.

TR’s picture

Title: Text comparison: Flags Support for Regular Expression » Text comparison: Support for Regular Expression Flags
Issue tags: +Needs tests, +Novice

Still in need of a patch here ...
Also need a test case.

TR’s picture

Version: 7.x-2.x-dev » 8.x-3.x-dev

We're not really adding new features this late in the D7 development cycle. Because there has been no community contribution for this feature in the form of a patch and/or test I'm moving this to D8 for consideration.

If you're interested in seeing this feature in Rules, please contribute a patch so we can review and discuss it.

zvonimirr’s picture

Here is a quick patch that allows you to write regex the old way or the new (custom) way which includes flags.

zvonimirr’s picture

zvonimirr’s picture

TR’s picture

Status: Active » Needs work

is_countable() is only available for PHP 7.3.0+, but Drupal 8 is still supposed to run with PHP 7.0, and even PHP 5.5 for some currently-supported versions of core.

There also needs to be new tests with this patch to test that flags work. Testing just one flag would be sufficient.

zvonimirr’s picture

Ah yes, I've added a test for the case-insensitive flag.

zvonimirr’s picture

zvonimirr’s picture

zvonimirr’s picture

@TR, #14 and #15 share the same name, but I accidentally uploaded the wrong version on #14. That's why it isn't tested.

zvonimirr’s picture

Status: Needs work » Needs review
TR’s picture

Status: Needs review » Needs work

I don't like the use of function_exists('is_countable') - that should be used only if there is absolutely no other way possible.

Note there are also a few easy-to-fix coding standards issues in the patch. See the test results for details.

zvonimirr’s picture

Status: Needs work » Needs review
zvonimirr’s picture

@TR do you think that the latest patch is good?