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

查看:15
本文介绍了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天全站免登陆