我有字符串,其输出结果可能是:
First : 14:15
Second: 14:2
Third: 10:2
Fourht: 1:20
Fifth: 1:5
我的正则表达式只匹配上面的第一个和第三个例子:
/(^[0-9]{2})/
如果有输出如1:10或1:2则没有输出(错误)。
通缉结果:
14
14
10
1
1
我可以使用preg_replace()或其他方法吗?我是否还需要更改我的正则表达式?如果是这样,我应该使用哪个正则表达式匹配“:”字符之前的数字?
感谢您提供信息和帮助!
答案 0 :(得分:2)
要重新声明,正则表达式不是这项工作的正确工具。
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem titredistance = menu.findItem(R.id.nav_distance);
SharedPreferences sharedPreferences = this.getSharedPreferences(
"appSharedPreferences", Context.MODE_PRIVATE);
String lat1 = sharedPreferences.getString("lat", null);
String lng1 = sharedPreferences.getString("lng", null);
if (ContextCompat.checkSelfPermission(Main2Activity.this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
LocationManager locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
Location location = locationManager.getLastKnownLocation(
locationManager.getBestProvider(criteria, false));
if (lat1 != null) {
if (lng1 != null) {
if (location != null) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
double latl = location.getLatitude();
double lngl = location.getLongitude();
double lat = Double.parseDouble(lat1);
double lng = Double.parseDouble(lng1);
Location locationA = new Location("point A");
locationA.setLatitude(lat);
locationA.setLongitude(lng);
Location locationB = new Location("point B");
locationB.setLatitude(latl);
locationB.setLongitude(lngl);
float distance = locationA.distanceTo(locationB);
dfi = Float.toString(distance);
titredistance.setTitle
("Distance entre la dernière position de Locate iT et la votre :"
+ "\n" + dfi + "mètres");
}
}
}
} else {
titredistance.setTitle("Aucune position de Locate iT enregistrée.");
}
}
return(true);
}
答案 1 :(得分:0)
搞定了!
preg_match_all('(^(.*):)', $string, $matches);
echo $matches[1][0]; // output is right! 14, 14, 10, 1, 1
https://regex101.com/r/aZ5uQ6/1
感谢您的想法!