我想搜索一个字符串,以确保它不包含任何数字或非字母字符。 从我之前的问题开始,提出了一个使用在线测试工具的Regex解决方案。 但我不能让它以这种格式工作:
<cfif REFindNoCase(".*?\\P{IsAlphabetic}.*", FORM.Forename)> <p>Error, your name contains illegal characters</p> </cfif>它只允许非法字符串通过,而不会接收非法字符。 所以myname1977似乎没有显示错误消息。 我究竟做错了什么?
I want to search a string to make sure it doesn't contain any numbers or non-alphabetic characters. From my previous question here a Regex solution was proposed which seems to work using online testing tools. But I can't get it to work in this format:
<cfif REFindNoCase(".*?\\P{IsAlphabetic}.*", FORM.Forename)> <p>Error, your name contains illegal characters</p> </cfif>It just allows illegal strings through without picking up on the illegal characters. So myname1977 seems to be let through without showing the error message. What am I doing wrong?
最满意答案
Reg Exp引擎ColdFusion使用的并不具备Java的所有功能。 这样的事情应该为你做:
<cfscript> input = "विकिपीडि"; writeDump(isValidUsername(input));// YES input = "विकिपीडि7"; writeDump(isValidUsername(input));// NO input = "hello"; writeDump(isValidUsername(input));// YES input = "hel lo"; writeDump(isValidUsername(input));// NO input = "hello7"; writeDump(isValidUsername(input));// NO function isValidUsername(input) { var pattern = ".*?\P{IsAlphabetic}.*"; var Matcher = createObject( "java", "java.util.regex.Pattern" ) .compile( javaCast( "string", pattern ) ) .matcher( javaCast( "string", input ) ); return !Matcher.matches(); } </cfscript>更新
我在评论中发布了这个,但可能有用,所以将它添加到答案中是有意义的:
如果您正在进行简单匹配,那么Java中的字符串内置了对Reg Exp的支持,因此您应该能够执行此操作: mystring.matches(".*?\P{IsAlphabetic}.*"); createObject版本更“可靠”,因为CF可能会在未来改变某些东西,从而破坏它。
The Reg Exp engine ColdFusion uses doesn't have all the power of Java. Something like this should do it for you:
<cfscript> input = "विकिपीडि"; writeDump(isValidUsername(input));// YES input = "विकिपीडि7"; writeDump(isValidUsername(input));// NO input = "hello"; writeDump(isValidUsername(input));// YES input = "hel lo"; writeDump(isValidUsername(input));// NO input = "hello7"; writeDump(isValidUsername(input));// NO function isValidUsername(input) { var pattern = ".*?\P{IsAlphabetic}.*"; var Matcher = createObject( "java", "java.util.regex.Pattern" ) .compile( javaCast( "string", pattern ) ) .matcher( javaCast( "string", input ) ); return !Matcher.matches(); } </cfscript>Update
I posted this in the comments but may be useful so adding it to the answer incase it's of interest:
If you're doing simple matching then strings in Java have built in support for Reg Exp so you should be able to just do this: mystring.matches(".*?\P{IsAlphabetic}.*"); The createObject version is more 'reliable' as CF may change something in the future which breaks it.
更多推荐
发布评论