正则表达式排列,无需重复 [英] Regex permutations without repetition

查看:70
本文介绍了正则表达式排列,无需重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个RegEx来检查是否可以在字符串中找到表达式.

I need a RegEx to check if I can find a expression in a string.

对于字符串"abc",我希望不重复地匹配任何排列的首次出现,在这种情况下为6:abc,acb,bac,bca,cab,cba.

For the string "abc" I would like to match the first appearance of any of the permutations without repetition, in this case 6: abc, acb, bac, bca, cab, cba.

例如,在此字符串"adesfecabefgswaswabdcbaes"中,它在位置7处发现一个巧合.

For example, in this string "adesfecabefgswaswabdcbaes" it'd find a coincidence in the position 7.

我也需要相同的排列方式,例如"abbc",而无需重复.这是12种情况:acbb,abcb,abbc,cab,cbab,cbba,bacb,babc,bcab,bcba,bbac,bbca

Also I'd need the same for permutations without repetition like this "abbc". The cases for this are 12: acbb, abcb, abbc, cabb, cbab, cbba, bacb, babc, bcab, bcba, bbac, bbca

例如,在此字符串"adbbcacssesfecabefgswaswabdcbaes"中,它在位置3处发现一个巧合.

For example, in this string "adbbcacssesfecabefgswaswabdcbaes" it'd find a coincidence in the position 3.

此外,我想知道类似情况的情况.

Also, I would like to know how would that be for similar cases.

编辑 我不是在寻找排列的组合,不是.我已经有那些.我要寻找的是一种检查这些排列是否在给定字符串中的方法.

EDIT I'm not looking for the combinations of the permutations, no. I already have those. WHat I'm looking for is a way to check if any of those permutations is in a given string.

编辑2 我认为这个正则表达式涵盖了我的第一个问题 ([abc])(?!\ 1)([abc])(?!\ 2 | \ 1)[abc]

EDIT 2 This regex I think covers my first question ([abc])(?!\1)([abc])(?!\2|\1)[abc]

可以在任何字符安全的情况下找到"abc"的所有排列(6).

Can find all permutations(6) of "abc" in any secuence of characters.

现在,当我有重复的字符(如abbc)(12个组合)时,我需要做同样的事情.

Now I need to do the same when I have a repeated character like abbc (12 combinations).

推荐答案

可能需要正则表达式"的唯一原因是,如果您使用的库或工具仅允许使用正则表达式指定某些类型的规则.例如,可以自定义某些编辑器,以特定方式为某些语法结构着色,并且它们仅允许将这些结构指定为正则表达式.

The only reason you might "need a regex" is if you are working with a library or tool which only permits specifying certain kinds of rules with a regex. For instance, some editors can be customized to color certain syntactic constructs in a particular way, and they only allow those constructs to be specified as regular expressions.

否则,您不需要需要正则表达式",而无需需要程序".这是一个:

Otherwise, you don't "need a regex", you "need a program". Here's one:

// are two arrays equal?
function array_equal(a1, a2) {
  return a1.every(function(chr, i) { return chr === a2[i]; });
}

// are two strings permutations of each other?
function is_permutation(s1, s2) {
  return array_equal(s1.split('').sort(), s2.split('').sort());
}

// make a function which finds permutations in a string
function make_permutation_finder(chars) {
  var len = chars.length;
  return function(str) {
    for (i = 0; i < str.length - len; i++) {
      if (is_permutation(chars, str.slice(i, i+len))) return i;
    }
    return -1; 
  };
}

> finder = make_permutation_finder("abc");
> console.log(finder("adesfecabefgswaswabdcbaes"));
< 6

正则表达式远远不够强大,无法执行此类操作.

Regexps are far from being powerful enough to do this kind of thing.

但是,有另一种选择,它是预先计算排列并构建一个动态的正则表达式来查找它们.您没有提供语言标签,但这是JS中的示例.假设您具有排列方式,而不必担心转义特殊的regexp字符,那只是

However, there is an alternative, which is precompute the permutations and build a dynamic regexp to find them. You did not provide a language tag, but here's an example in JS. Assuming you have the permutations and don't have to worry about escaping special regexp characters, that's just

regexp = new RegExp(permuations.join('|'));

这篇关于正则表达式排列,无需重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆