正则表达式 WORD 的最后一个字符 [英] regex last character of a WORD

查看:110
本文介绍了正则表达式 WORD 的最后一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试匹配 WORD 中的最后一个字符.

I'm attempting to match the last character in a WORD.

一个 WORD 是一个非空白字符序列'[^\n\r\t\f ]',或与 ^$ 匹配的空行.

A WORD is a sequence of non-whitespace characters '[^\n\r\t\f ]', or an empty line matching ^$.

我这样做的表达是:"[^ \n\t\r\f]\(?:[ \$\n\t\r\f]\)"

The expression I made to do this is: "[^ \n\t\r\f]\(?:[ \$\n\t\r\f]\)"

正则表达式匹配跟在空白字符或行尾的非空白字符.

The regex matches a non-whitespace character that follows a whitespace character or the end of the line.

但我不知道如何阻止它从结果中排除以下空白字符以及为什么它似乎没有捕获行尾之前的字符.

But I don't know how to stop it from excluding the following whitespace character from the result and why it doesn't seem to capture a character preceding the end of the line.

使用字符串Hi World!",我期望:i"和!"被捕获.

Using the string "Hi World!", I would expect: the "i" and "!" to be captured.

相反,我得到:我".

我可以采取哪些步骤来解决这个问题?

What steps can I take to solve this problem?

推荐答案

Word"即非空白字符序列方案

注意[^ \n\t\r\f](?:[ \$\n\t\r\f]) 仍然匹配(消耗)空白字符(因此,它成为匹配的一部分)并且它在字符串末尾不匹配为 $ 符号不是字符类中的字符串结束锚,它被解析为文字 $ 符号.

Note that a non-capturing group (?:...) in [^ \n\t\r\f](?:[ \$\n\t\r\f]) still matches (consumes) the whitespace char (thus, it becomes a part of the match) and it does not match at the end of the string as the $ symbol is not a string end anchor inside a character class, it is parsed as a literal $ symbol.

你可以使用

\S(?!\S)

查看正则表达式演示

\S 匹配后面没有非空白字符的非空白字符(由于 (?!\S) 负前瞻).

The \S matches a non-whitespace char that is not followed with a non-whitespace char (due to the (?!\S) negative lookahead).

一般词"案例

如果一个词只由字母、数字和下划线组成,也就是说,如果它与\w+匹配,你可以简单地使用

If a word consists of just letters, digits and underscores, that is, if it is matched with \w+, you may simply use

\w\b

这里,\w 匹配一个单词"字符,单词边界断言后面没有字符字符.

Here, \w matches a "word" char, and the word boundary asserts there is no word char right after.

请参阅另一个正则表达式演示.

这篇关于正则表达式 WORD 的最后一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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