[英]How to remove words with # or $ at the end of a sentence to be removed (Regex)
I have the following:我有以下内容:
The moment #BTC, we have all $BTC been waiting for:We are happy to announce NIX Platform is rebranding to.. $NBT > $VOICE $NIX > $MUTE $ETH $BTC #BTC
I would like to remove only the remove the words with # or $ to be removed from the end, not the middle, so the above string would look like this我只想删除带有 # 或 $ 的单词要从末尾删除,而不是从中间删除,所以上面的字符串看起来像这样
The moment #BTC, we have all $BTC been waiting for:We are happy to announce NIX Platform is rebranding to.. > >
Currently I have #(??(:?hashtag)\b)[\w-]+(?=(::\s+#[\w-]+)*\s*$)
as regex that removes words with # from end but not $, not sure what I need to include those as well目前我有
#(??(:?hashtag)\b)[\w-]+(?=(::\s+#[\w-]+)*\s*$)
作为正则表达式,它使用 # 删除单词从头开始,但不是 $,不确定我还需要包含哪些内容
An alternative regex with a word boundary and negative lookahead to disallow any word character following a whitespace:具有单词边界和负前瞻的替代正则表达式,以禁止空格后面的任何单词字符:
[#$@]\w+\b(?!\W*\s\w)
RegEx Explanation:正则表达式解释:
[#$@]
: Match #
or $
or @
[#$@]
: 匹配#
或$
或@
\w+b
: Match 1+ word characters followed by word boundary \w+b
:匹配 1+ 个单词字符,后跟单词边界(?!\W*\s\w)
: Negative lookahead to assert that we don't have a word character after a whitespace ahead of current position (?!\W*\s\w)
:否定前瞻断言我们在当前 position 前面的空格后没有单词字符Alternatively , you can also use an *atomic group` to disallow backtracking:或者,您也可以使用 *atomic group` 来禁止回溯:
[#$@](?>\w+)(?!\W*\s\w)
You could use a positive lookahead and assert that until the end of the string all words start with a non word character.您可以使用积极的前瞻并断言直到字符串结尾所有单词都以非单词字符开头。
[#$][^\s$#]+(?=(?:\s+[^\w\s]\S*)*\s*$)
[#$][^\s$#]+
Match either #
or $
and 1+ occurrences of any char except #
or $
[#$][^\s$#]+
匹配#
或$
和 1+ 次出现的任何字符,除了#
或$
(?=
Positive lookahead, assert on the right (?=
正向前瞻,在右边断言
(?:\s+[^\w\s]\S*)*
Optional repetitions of 1 or more whitespace chars followed by any char except a whitspace or char char (?:\s+[^\w\s]\S*)*
可选重复 1 个或多个空白字符,后跟除 whitspace 或 char 字符外的任何字符\s*
Match optional trailing whitspace chars \s*
匹配可选的尾随空白字符$
End of string $
字符串结尾)
Close lookahead )
关闭前瞻If the start with $
or #
can contain the other one, another option could be using a tempered greedy token approach with a capture group and a backreference \1
.如果以
$
或#
开头可以包含另一个,另一种选择可能是使用带有捕获组和反向引用\1
的缓和贪婪令牌方法。
To prevent partial matches, you might add asserting a whitespace boundary at the left.为了防止部分匹配,您可以在左侧添加断言空白边界。
(?<!\S)([#$])(?:(?!\1)\S)*(?=(?:\s+[^\w\s]\S*)*\s*$)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.