如何我发现字符串包含重复字符多于6次Flex中? [英] How I find that string contain a repeating character more then 6 time in Flex?

查看:198
本文介绍了如何我发现字符串包含重复字符多于6次Flex中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么找到该字符串包含重复字符多于6次Flex中? 在我的情况下,用户输入的只有数字(0-9)和我做对美国传真号码验证。 像11111112255,225555555522等

How I find that string contain a repeating character more then 6 time in Flex? In my case the user input is only digits (0-9) and i am doing for US Fax no validation. like 11111112255 , 225555555522 etc

推荐答案

一个普通EX pression方式:

A regular expression way:

/(.)\1{6}/

这将搜索的任意的角色(不一定是数字)重复7次。

This will search for any character (not necessarily digit) repeated 7 times.

/(\d)\1{6}/

同样的,但数字而已。

The same, but for digits only.

有技术,这将一般比正则表达式更快,例如,稍加修改 Bitap 的算法可能被证明是更快的。

There are techniques that will be generally faster than regexp, for example, slightly modified Bitap algorithm may prove to be faster.

下面是Bitup算法的一个修改过的版本来搜索重复字符串中的字符:

Here's an altered version of Bitup algorithm to search for repeating characters in a string:

package
{
    import flash.display.Sprite;

    public class BitapConsequent extends Sprite
    {
        public function BitapConsequent()
        {
            super();
            test();
        }

        private function test():void
        {
            // 10 -1 3 0
            trace(bitapConsequent("---####$$$^^^^^^", 6),
                bitapConsequent("---####$$$^^^^^^", 7),
                bitapConsequent("---####$$$^^^^^^", 4),
                bitapConsequent("---####$$$^^^^^^", 3));
        }

        private static function bitapConsequent(
            text:String, consequent:uint):int
        {
            // Unless found, the result is negative
            var result:int = -1;
            var len:int = text.length;
            var offset:int;
            var reverse:int;

            if (len >= consequent)
            {
                consequent--;
                len -= consequent;
                // loop through the whole string sans
                // the substring which is shorter than
                // the number of characters that have to
                // be the same
                outer: for (; offset < len; offset++)
                {
                    // jump to the farmost end of the possible
                    // match and walk back checking that each
                    // two characters match, i.e. if this is
                    // the array of characters a = ['a', 'b', 'b']
                    // then first check if a[2] == a[1], then
                    // if a[1] == a[0], if characters are equal,
                    // continue to the next iteration, else--
                    // restart the search
                    for (reverse = offset + consequent;
                        reverse > offset; reverse--)
                    {
                        if (text.charAt(reverse) !== 
                            text.charAt(reverse - 1))
                            continue outer;
                    }
                    // If we got here, we've found `consequent'
                    // equal characters, so terminate the loop
                    result = offset;
                    break;
                }
            }
            return result;
        }
    }
}

在早期版本中确实使用bitup算法,但是经过一番思考,我意识到它不是必需的,所以这是一个比较完善的版本,这也并不限制一到32个字符只匹配。

The earlier version indeed used bitup algorithm, but after some thought, I've realized it wasn't required, so this is a bit more refined version, which also doesn't limit one to the 32-characters matches only.

这篇关于如何我发现字符串包含重复字符多于6次Flex中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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