Flex(ActionScript 3):如何查找字符串包含重复字符超过6次的字符串? [英] Flex (ActionScript 3): How to find that a string contain a repeating character more than 6 times?

查看:65
本文介绍了Flex(ActionScript 3):如何查找字符串包含重复字符超过6次的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Flex中,我如何发现该字符串包含重复字符超过6次?就我而言,用户输入仅是数字(0-9),而我正在为美国传真做验证.

How I find that string contains a repeating character more than 6 times 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

推荐答案

正则表达式:

/(.)\1{6}/

这将搜索重复7次的任何个字符(不一定是数字).

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

/(\d)\1{6}/

相同,但仅适用于数字.

The same, but for digits only.

有些技术通常会比regexp更快,例如,对 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.

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

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