如果我在表达式的开头有一个带括号的字符串,我将如何使用正则表达式将它们与数字一起删除.例如,我有字符串A:
"(1) Hi How are you (2).
我希望它看起来像这样:字符串B:
Hi How Are you (2).
我试过用
string.replaceAll( "^\p {P}", "");
但这只做到了这一点:
1)你好,你好(2).
它只删除了表达式中的第一个括号.
你可以做:
string = string.replaceFirst("^\\s*\\(\\d*\\)\\s*", "");
Run Code Online (Sandbox Code Playgroud)
说明:
^ # match line start
\\s* # match 0 or more spaces
\\( # match left (
\\d* # match 0 or more digits
\\) # match right )
\\s* # match 0 or more spaces
Run Code Online (Sandbox Code Playgroud)
替换是空字符串.