我如何在Flex中发现该字符串包含多于6次的重复字符? 在我的情况下,用户输入只是数字(0-9),我正在做美国传真没有验证。 如11111112255,2255555555522等
答案 0 :(得分:3)
正则表达方式:
/(.)\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个字符的匹配。
答案 1 :(得分:1)
您可以使用正则表达式:
var testString:String = "1111111";
if ( testString.search( /[0-9]{7}/ ) != -1 )
trace( "The string '" + testString + "' contains more than 6 repeating digits" );
修改强>
正如@wvxvw指出的那样,如果你的字符串类似于1112222
或1234567
,那么这将会破坏 - 他的正则表达式可以解决这个问题
答案 2 :(得分: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>