gpt4 book ai didi

RegEx:将所有内容匹配到最后一个空格而不包括它

转载 作者:行者123 更新时间:2023-12-04 23:17:37 24 4
gpt4 key购买 nike

我想将字符串中的所有内容匹配到最后一个空格,但不包括它。例如,我想匹配我放入 中的字符。粗体 :

RENATA T. GROCHAL

So far I have ^(.+\s)(.+) However, it matches the last space and I don't want it to. RegEx should work also for other languages than English, as mine does.

EDIT: I didn't mention that the second capturing group should not contain a space – it should be GROCHAL not GROCHAL with a space before it.

EDIT 2: My new RegEx based on what the two answers have provided is: ^((.+)(?=\s))\s(.+) and the RegEx used to replace the matches is \3, \1. It does the expected result:

GROCHAL, RENATa T.

任何改进都是可取的。

最佳答案

^(.+)\s(.+)
带替换字符串:
\2, \1
更新:

另一个可以折叠 2 个捕获组之间的额外空间的版本:

^(.+?)\s+(\S+)$

关于RegEx:将所有内容匹配到最后一个空格而不包括它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36247497/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com