如何使用正则表达式提取子字符串
我有一个字符串,其中有两个单引号 ' 字符。 在单引号之间是我想要的数据。 如何编写正则表达式从以下文本中提取“我想要的数据”? mydata = "some string with 'the data i want' inside"; #1楼 您不需要正则表达式。 将apache commons lang添加到您的项目( http://commons.apache.org/proper/commons-lang/ ),然后使用: String dataYouWant = StringUtils.substringBetween(mydata, "'"); #2楼 有一个简单的方法: String target = myData.replaceAll("[^']*(?:'(.*?)')?.*", "$1"); 通过使匹配组为可选,这还可以通过在这种情况下返回空白来解决找不到引号的问题。 观看 现场演示 。 #3楼 String dataIWant = mydata.split("'")[1]; 观看 现场演示 #4楼 String dataIWant = mydata.replaceFirst(".*'(.*?)'.*", "$1"); #5楼 如在javascript中: mydata.match(/'([^']+)'/)[1] 实际的正则表达式为: /'([^']+)'/