RegEx为西班牙字母在ColdFusion 8 [英] RegEx for Spanish alphabets in ColdFusion 8

查看:148
本文介绍了RegEx为西班牙字母在ColdFusion 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用[az]来检查CF 8中从a到z的任何字母表。但是,是否有任何正则表达式来检测á,í,ó,é,ñ等西班牙字母? / p>

感谢提前,
Monte

解决方案

ColdFusion很好地处理Unicode正则表达式。
你可以使用 #Chr(375)#来将字符转换为正则表达式字符串,但是这样做有点乱。



然而,Java使用Unicode,因为CF可以轻松地利用Java,你可以使用Java正则表达式进行unicode匹配。





这将与Java regex中的单个Unicode字母匹配:

  \p { L} 

有关regex Unicode的更多详细信息,请访问: http://www.regular-expressions.info/unicode.html





并且对于在CF中使用Java正则表达式,很好的简单替换就是这样:

  ; cfset NewString = OldString.replaceAll('\p {L}','ReplaceWith')/> 

所以如果你需要的是替换字符串,你可以这样做。

$ b但是,如果你想要匹配(等效于rematch)或更复杂的功能,那么最简单的解决方案是使用一个组件,将Java regex功能包装到一个易于使用的CFC中,并具有常规的CFML功能你可以打电话。如 jre-utils.cfc



这可以让你做:

 < cfset jre = createObject('component','jre-utils ').init()/> 

< cfset Matches = jre.match('\p {L} ++',String)/>

这将返回字符串中的Unicode字符数组。





I know that I can use [a-z] to check for any alphabets from a to z in CF 8. However, are there any regex to detect spanish alphabets like á, í, ó, é, ñ, etc.?

Thanks in advance, Monte

解决方案

ColdFusion doesn't nicely deal with Unicode regex. You can use things like #Chr(375)# to get the characters into a regex string, but it's a bit messy having to do that.

However, Java does work with Unicode, and since CF can utilise Java easily, you can use Java regexes to do unicode matching.


This will match a single Unicode letter in Java regex:

\p{L}

With more details on regex Unicode here: http://www.regular-expressions.info/unicode.html


And as for using Java regex in CF, well simple replacing is just this:

<cfset NewString = OldString.replaceAll('\p{L}','ReplaceWith') />

So if all you need is to replace strings, you can do that.

However, if you want matching (equivalent to rematch), or more complex functionality, then simplest solution is to use a component that wraps the Java regex functionality into a easy to use CFC with regular CFML functions you can call. Like jre-utils.cfc

This allows you to do:

<cfset jre = createObject('component','jre-utils').init() />

<cfset Matches = jre.match( '\p{L}++' , String ) />

Which will return an array of the (Unicode) words in the string.


这篇关于RegEx为西班牙字母在ColdFusion 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆