重新放置任何以@开头的单词

时间:2021-06-06 09:57:27

I'm trying to rereplace any words that begin with @ in my string...

我正试图重新放置在我的字符串中以@开头的任何单词...

I've tried a number of variations, but none of them seem to be working...

我尝试了很多变化,但它们似乎都没有工作......

rereplace(getMessages.term, "[\s]?\B@\w+", "", "ALL")

Any suggestions?

有什么建议么?

1 个解决方案

#1


3  

CF's built-in regex doesn't support look-behinds, which is what you need to achieve this (since you want to look "behind" (before) the @ and verify what is/isn't there, without including it in your match).

CF的内置正则表达式不支持后视,这是你需要实现这一目标(因为你想看看@之前“(之前)@并验证是什么/不存在,而不是在你的比赛)。

However you can easily dip into Java, to make use of Java's regex support (which does support look-behinds), as simply as this:

但是,您可以轻松地使用Java,以利用Java的正则表达式支持(它支持后视图),就像这样:

<cfset Text = Text.replaceAll('(?<!\w)@\w+','') />

The (?<!\w) part is a negative look-behind saying "make sure there is no \w before this position".

(?<!\ w)部分是一个负面的背后说“确保在这个位置之前没有\ w”。

You might also want to consider using (?<!\S) which will prevent any non-whitespace character, or if you need to match specific characters then use (?<![a-z_\-.,]) or whatever.

您可能还想考虑使用(?<!\ S)来阻止任何非空格字符,或者如果您需要匹配特定字符,则使用(?<![a-z _ \ - 。,])或其他。

#1


3  

CF's built-in regex doesn't support look-behinds, which is what you need to achieve this (since you want to look "behind" (before) the @ and verify what is/isn't there, without including it in your match).

CF的内置正则表达式不支持后视,这是你需要实现这一目标(因为你想看看@之前“(之前)@并验证是什么/不存在,而不是在你的比赛)。

However you can easily dip into Java, to make use of Java's regex support (which does support look-behinds), as simply as this:

但是,您可以轻松地使用Java,以利用Java的正则表达式支持(它支持后视图),就像这样:

<cfset Text = Text.replaceAll('(?<!\w)@\w+','') />

The (?<!\w) part is a negative look-behind saying "make sure there is no \w before this position".

(?<!\ w)部分是一个负面的背后说“确保在这个位置之前没有\ w”。

You might also want to consider using (?<!\S) which will prevent any non-whitespace character, or if you need to match specific characters then use (?<![a-z_\-.,]) or whatever.

您可能还想考虑使用(?<!\ S)来阻止任何非空格字符,或者如果您需要匹配特定字符,则使用(?<![a-z _ \ - 。,])或其他。