gpt4 book ai didi

java正则表达式提取直到特殊字符

转载 作者:行者123 更新时间:2023-12-04 05:53:08 25 4
gpt4 key购买 nike

String str = "Text0TEXT1.more text ";
String str = "Text0TEXT1(more text ";
String str = "Text0TEXT1{more text ";

如果我有一行结尾可能是几个字符,例如 .或 ( 或 { 或 ; 我怎样才能只提取 TEXT1 ?

更新 : 在文本 1 之前有文本 0,特殊字符可能存在也可能不存在

更新 2
String str = "Beginning text Text I want . Text I don't want"
String str = "Beginning text with numbers Text I want ( Text I don't want )"
String str = "Beginning text with numbers Text I want { Text I don't want }"

我需要提取“我想要的文本”,但直到最后我才能得到其余的文本。特殊字符是 . ( { :

最佳答案

怎么样:

^(?:[a-zA-Z ]+[0-9]+ )?([a-zA-Z ,]+)

您想要的文本在第 1 组中。

说明:
^                 : begining of string
(?: : start non capture group
[a-zA-Z ]+ : one or more letter or space
[0-9]+ : one or more digit
: a space
)? : end of group optional
( : start capture group 1
[a-zA-Z ,]+ : one or more letter, sapce or coma
) : end of group

关于java正则表达式提取直到特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9826858/

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