Java Regex删除开始/结束单引号但留下引号

Geo*_*rge 7 java regex csv replace

我有来自CSV文件的数据,该文件用单引号括起来,例如:

'Company name'
'Price: $43.50'
'New York, New York'
Run Code Online (Sandbox Code Playgroud)

我希望能够在值的开头/结尾替换单引号,但在数据中保留引号,例如:

'Joe's Diner'  should become Joe's Diner
Run Code Online (Sandbox Code Playgroud)

我可以

updateString = theString.replace("^'", "").replace("'$", "");
Run Code Online (Sandbox Code Playgroud)

但我想知道我是否可以将它组合起来只做一次替换.

Jos*_*iah 16

您可以使用运算符.

updateString = theString.replaceAll("(^')|('$)","");
Run Code Online (Sandbox Code Playgroud)

看看它是否适合你:)