为什么这个正则表达式不能在REFind()中工作?(Why won't this Regex work in REFind()?)

编程入门 行业动态 更新时间:2024-10-23 07:40:23
为什么这个正则表达式不能在REFind()中工作?(Why won't this Regex work in REFind()?)

我想搜索一个字符串,以确保它不包含任何数字或非字母字符。 从我之前的问题开始,提出了一个使用在线测试工具的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.

更多推荐

本文发布于:2023-08-04 23:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1424217.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:能在   工作   正则表达式   REFind   Regex

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!