Actionscript 3 Flex(ActionScript 3):如何发现字符串包含重复字符超过6次?
我如何在Flex中发现字符串包含重复字符超过6次? 在我的情况下,用户输入仅为数字0-9,我正在为我们进行传真无验证Actionscript 3 Flex(ActionScript 3):如何发现字符串包含重复字符超过6次?,actionscript-3,flash,apache-flex,air,flash-builder,Actionscript 3,Flash,Apache Flex,Air,Flash Builder,我如何在Flex中发现字符串包含重复字符超过6次? 在我的情况下,用户输入仅为数字0-9,我正在为我们进行传真无验证 like 11111112255, 225555555522, etc 您可以使用正则表达式: var testString:String = "1111111"; if ( testString.search( /[0-9]{7}/ ) != -1 ) trace( "The string '" + testString + "' contains more than
like 11111112255, 225555555522, etc
您可以使用正则表达式:
var testString:String = "1111111";
if ( testString.search( /[0-9]{7}/ ) != -1 )
trace( "The string '" + testString + "' contains more than 6 repeating digits" );
编辑:
正如@wvxvw所指出的,如果您的字符串类似于1112222或1234567,则会出现这种情况-他的正则表达式可以绕过这一点您可以使用正则表达式:
var testString:String = "1111111";
if ( testString.search( /[0-9]{7}/ ) != -1 )
trace( "The string '" + testString + "' contains more than 6 repeating digits" );
编辑:
正如@wvxvw指出的,如果您的字符串类似于1112222或1234567,则会断开-他的正则表达式以正则表达式的方式绕过它:
/(.)\1{6}/
这将搜索任何不一定重复7次的字符
/(\d)\1{6}/
相同,但仅适用于数字
有一些技术通常比regexp更快,例如,稍微修改一下的算法可能会更快
下面是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{6}/
这将搜索任何不一定重复7次的字符
/(\d)\1{6}/
相同,但仅适用于数字
有一些技术通常比regexp更快,例如,稍微修改一下的算法可能会更快
下面是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个字符的匹配项。我这样做
<?xml version="1.0" encoding="utf-8"?>
我是这样做的
<?xml version="1.0" encoding="utf-8"?>
签出-右侧的社区选项卡了解更多示例在我的情况下,我的金额始终是1112222或1234567,请为我提供该类型金额的确切regExp。@Tahir:以wvxvw提供的regex:-/\d\1{6}/签出-右侧的社区选项卡了解更多示例在我的情况下,我的金额始终是1112222或1234567,请为该类型的金额提供准确的regExp。@Tahir:以wvxvw:-/\d\1{6}/ok提供的regex为例,例如:对于每个数字,如果后面的6个相同,则匹配?好,例如:对于每个数字,如果后面的6个相同,则匹配?