Flex で 6 回を超える繰り返し文字が文字列に含まれていることを確認するにはどうすればよいですか? 私の場合、ユーザー入力は数字 (0 ~ 9) のみであり、米国の Fax に対して検証を行っていません。
like 11111112255, 225555555522, etc
Flex で 6 回を超える繰り返し文字が文字列に含まれていることを確認するにはどうすればよいですか? 私の場合、ユーザー入力は数字 (0 ~ 9) のみであり、米国の Fax に対して検証を行っていません。
like 11111112255, 225555555522, etc
正規表現の方法:
/(.)\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;
}
}
}
以前のバージョンは確かにビットアップアルゴリズムを使用していましたが、少し考えてみると、それは必要ないことに気づきました。これはもう少し洗練されたバージョンであり、32文字の一致のみに制限されていません。
正規表現を使用できます:
var testString:String = "1111111";
if ( testString.search( /[0-9]{7}/ ) != -1 )
trace( "The string '" + testString + "' contains more than 6 repeating digits" );
編集:
@wvxvwが指摘しているように、文字列がまたはのようなものである場合、これは壊れます1112222
-1234567
彼の正規表現はそれを回避します
私はこのようにこれを行います、
<?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>