我怎么找到该字符串包含重复字符多于6次在Flex中?(How I find that string

2019-09-29 11:24发布

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

Answer 1:

正则表达式的方法:

/(.)\1{6}/

这将搜索重复任何字符(不一定是数字)的7倍。

/(\d)\1{6}/

同样的,但仅数字。

有技术,这将是通常比的regexp更快,例如,稍加修改Bitap算法可能被证明是更快。

这里的Bitup算法来搜索字符串重复字符的修改过的版本:

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个字符只匹配。



Answer 2:

您可以使用正则表达式:

var testString:String = "1111111";
if ( testString.search( /[0-9]{7}/ ) != -1 )
    trace( "The string '" + testString + "' contains more than 6 repeating digits" );

编辑:

作为@wvxvw指出,这将打破,如果你的字符串是一样的东西11122221234567 -他的正则表达式得到周围



Answer 3:

我这样做,这样,

<?xml version="1.0" encoding="utf-8"?>

<fx:Script>
    <![CDATA[
        import mx.controls.Alert;
        protected function button1_clickHandler(event:MouseEvent):void
        {
            if(id_input.text.length >=10)
            {
                for(var i:uint=0; i<4; i++)
                {
                    var counter:int = 0;
                    var str:String = id_input.text.substr(i,1);
                    var index:uint = 0;
                    index = i;
                    index += 1;
                    //case 1
                    if(str == id_input.text.substr(index ,1))
                        counter ++;
                    index += 1;
                    //case 2
                    if(str == id_input.text.substr(index ,1))
                        counter ++;
                    index += 1;
                    //case 3
                    if(str == id_input.text.substr(index ,1))
                        counter ++;
                    index += 1;
                    //case 4
                    if(str == id_input.text.substr(index ,1))
                        counter ++;
                    index += 1;
                    //case 5
                    if(str == id_input.text.substr(index ,1))
                        counter ++;
                    index += 1;
                    //case 6
                    if(str == id_input.text.substr(index ,1))
                        counter ++;
                    if(counter >= 6){
                        Alert.show('More then 6 char present in string');
                        break;
                    }
                }
            }
        }

    ]]>
</fx:Script>

<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<s:VGroup width="100%" height="100%">

    <s:TextInput id="id_input"/>
    <s:Button label="Validate" click="button1_clickHandler(event)" />

</s:VGroup>



文章来源: How I find that string contain a repeating character more then 6 time in Flex?