String str = "POLYGON((39.4189453125 37.418708616699824,42.0556640625 37.418708616699824,43.4619140625 34.79181436843146,38.84765625 33.84817790215085,39.4189453125 37.418708616699824))";
我试图只获得39.4189453125 37.418708616699824,42.0556640625 37.418708616699824,43.4619140625 34.79181436843146,38.84765625 33.84817790215085,39.4189453125 37.418708616699824
与
final String coordsOnly = str.replaceAll("\\D\\(\\(\\w\\)\\)", "$2");
但我得到coordsOnly = "POLYGON((39.4189453125 37.418708616699824,42.0556640625 37.418708616699824,43.4619140625 34.79181436843146,38.84765625 33.84817790215085,39.4189453125 37.418708616699824))"
我错过了什么?
答案 0 :(得分:3)
这里的一个合理方法是匹配以下模式,然后用第一个捕获组替换all:
POLYGON\(\((.*?)\)\)
示例代码:
final String coordsOnly = str.replaceAll("POLYGON\\(\\((.*?)\\)\\)", "$1");
修改强>
如果您还需要隔离数字对,则可以在逗号上使用String#split()
。实际上,这看起来像数据库查询的输出,我怀疑数据库可能提供更好的方法来获取单个值。但是,如果您无法获得所需的确切输出,此处给出的答案是您的选择。
答案 1 :(得分:2)
也许这就是你想要的?:
String rex = "[+-]?([0-9]*[.])?[0-9]+";
Pattern p = Pattern.compile(rex);
String input = "POLYGON((39.4189453125 37.418708616699824,42.0556640625 37.418708616699824,43.4619140625 34.79181436843146,38.84765625 33.84817790215085,39.4189453125 37.418708616699824))";
Matcher matcher = p.matcher(input);
while(matcher.find()) {
System.out.println(matcher.group());
}
答案 2 :(得分:2)
实际上,很多。
"\\D" // Matches a non-digit character, but it only matches one,
// while you need to match a word "POLYGON";
"\\(\\(" // Good. Matches the double left parentheses ((
"\\w" // One word character? Same issue, you need to match multiple chars. And what about '.'?
"\\)\\)" // Good. Matches the double right parentheses ))
并且转义()
不会创建匹配的组;并且\\w
只匹配一个字符[a-zA-Z_0-9]
,它甚至不匹配.
。
我相信你应该尝试这样的事情:
String coords = str.replaceAll("POLYGON\\(\\(([^)]+)\\)\\)", "$1");
答案 3 :(得分:1)
试试这个版本:
str = str.replaceAll("[^\\d\\.\\s,]", "");
答案 4 :(得分:1)
//Create object for numbering name attributes of lift and cardio rows
var nameNumberManager = {
reNameNumberAll: function(){
alert(1);
var liftRows = this.getLiftRows();
alert(2);
this.reNameNumberLiftRows(liftRows);
alert(3);
var cardioRows = this.getCardioRows();
alert(4);
this.reNameNumberCardioRows(cardioRows);
alert(5);
// get all cardio rows
},
getLiftRows: function(){
var liftRows = document.getElementById('liftrows');
return liftRows;
},
getCardioRows: function(){
var cardioRows = document.getElementById('cardiorows');
return cardioRows;
},
reNameNumberLiftRows: function(liftRows){
for(var i=0; i<liftRows.length; i++){
var liftField = liftRows.getElementsByClassName('input').getElementsByClassName('lift')[0];
var setField = liftRows.getElementsByClassName('setfield');
var repFields = liftRows.getElementsByClassName('repfield');
var weightFields = liftRows.getElementsByClassName('weightfield');
liftField.name = 'lift_string'+i;
setField.name = 'sets'+i;
for(var j=0; i<repFields.length; j++){
repFields[i].name = 'reps'+i;
}
for(var j=0; i<weightFields.length; j++){
weightFields[i].name = 'weight'+i;
}
}
},
reNameNumberCardioRows: function(cardioRows){
for(var i=0; i<cardioRows.length; i++){
var activityField = cardioRows.getElementsByClassName('dropdowntext');
var minField = cardioRows.getElementsByClassName('minfield');
var distField = cardioRows.getElementsByClassName('distfield');
activityField.name = 'cardio_string'+i;
minField.name = 'minutes'+i;
distField.name = 'distance'+i;
}
},
}
输出:
39.4189453125 37.418708616699824,42.0556640625 37.418708616699824,43.4619140625 34.79181436843146,38.84765625 33.84817790215085,39.4189453125 37.418708616699824
答案 5 :(得分:0)
另一种选择是替换所有非数字,空格,点或逗号:
str.replaceAll("[\\D&&\\S&&[^,\\.]]", "")
输出:
39.4189453125 37.418708616699824,42.0556640625 37.418708616699824,43.4619140625 34.79181436843146,38.84765625 33.84817790215085,39.4189453125 37.418708616699824