Java正则表达式删除开始/结束单引号但保留内部引号
我有来自 CSV 文件的数据,这些数据用单引号引起来,例如:
'Company name'
'Price: $43.50'
'New York, New York'
我希望能够替换值开头/结尾处的单引号,但在数据中保留引号,例如:
'Joe's Diner' should become Joe's Diner
我可以做
updateString = theString.replace("^'", "").replace("'$", "");
,但我想要知道我是否可以将其组合起来只进行一次替换。
I have data from a CSV file that is enclosed in single quotes, like:
'Company name'
'Price: $43.50'
'New York, New York'
I want to be able to replace the single quotes at the start/end of the value but leave quotes in the data, like:
'Joe's Diner' should become Joe's Diner
I can do
updateString = theString.replace("^'", "").replace("'$", "");
but I wanted to know if I could combine it to only do one replace.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用或运算符。
看看这是否适合你:)
You could use the or operator.
See if that works for you :)
请注意,您没有的表单将不起作用,因为
replace
使用文字字符串,而不是正则表达式。这是通过使用捕获组
(.*)
来实现的,在替换文本中用$1
引用该组。你也可以这样做:当然,如果你确定开头和结尾都有单引号,最简单的解决方案是:
Note that the form you have no won't work because
replace
uses literal strings, not regexes.This works by using a capturing group
(.*)
, which is referred to with$1
in the replacement text. You could also do something like:Of course, if you're certain there's a single quote at the beginning and end, the simplest solution is:
您可以使用正则表达式删除数字/数字周围的双引号。
如果存在负数,上述方法将不起作用。
对于负数,正则表达式会有点复杂,如下所示。但我没试过。
You can use regex to remove double quotes around digits/numbers.
above will not work if negative numbers are present.
for negative numbers, the regex will be a little complex like below. But I haven't tried it.