1

我如何在 Flex 中发现该字符串包含超过 6 次的重复字符?在我的情况下,用户输入只有数字(0-9),我正在为美国传真做没有验证。

like 11111112255, 225555555522, etc
4

3 回答 3

3

一种正则表达式方式:

/(.)\1{6}/

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

/(\d)\1{6}/

相同,但仅适用于数字。

有些技术通常会比正则表达式更快,例如,稍微修改的 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个字符。

于 2012-11-02T12:31:43.560 回答
1

您可以使用正则表达式:

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

编辑:

正如@wvxvw 指出的那样,如果您的字符串类似于11122221234567- 他的正则表达式解决了这个问题,这将中断

于 2012-11-02T10:58:16.870 回答
0

我这样做是这样的,

<?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>

于 2012-11-04T09:49:52.240 回答