使用正则表达式删除单词开头或结尾的撇号 [英] Remove an apostrophe at the beginning or at the end of a word with regex

查看:51
本文介绍了使用正则表达式删除单词开头或结尾的撇号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含单词的 String 列表.某些单词在开头或结尾都有单引号,例如:

I have a list of String that contains word. Some words have apostrophe at the beginning or/and at the end like this:

apple
'orange
banana'
joe's

我只想删除像这样的单词的开头和结尾的撇号:

I want to delete only the apostrophes at the beginning and at the end of the words like this:

apple
orange
banana
joe's

我已经尝试过以下正则表达式,但不起作用:

I have tried with the following regex but it doesn't work:

myString.replaceAll("((?<=^)'|'(?=$))", "");

它在IntelliJ中不起作用:

It does not work in IntelliJ:

但是它可以与

为什么正则表达式不起作用,如何删除开头和结尾的撇号?

谢谢您的帮助!

推荐答案

我了解我的错误,我忘记了分配作业...

I understand my mistake, I forgot to make the assignment…

myString = myString.replaceAll("(\B'\b)|(\b'\B)", "");

谢谢你,很抱歉这个愚蠢的问题.

Thank you and sorry for this dumb question.

这篇关于使用正则表达式删除单词开头或结尾的撇号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆