您好我已阅读使用正则表达式但我无法理解如何使用它。
我想拆分一个字符串来制作一个列表,按空格分割,除非用##或引号&#34包围; "
values = '2 #room 2.# 5 1 -1 -1'
或values = '2 "room 2." 5 1 -1 -1'
只使用split()会导致:
['2', '#room', '2.#', '5', '1', '-1', '-1']
我希望输出没有#的房间名称,并且因为空间而没有分开:
['2', 'room 2.', '5', '1', '-1', '-1']
答案 0 :(得分:1)
您可以执行以下操作(将#替换为“然后使用shelex拆分)
import shlex
values = '2 #room 2.# 5 1 -1 -1'
print(shlex.split(values.replace('#','"')))
输出
['2', 'room 2.', '5', '1', '-1', '-1']
基于 Casimir et Hippolyte 的精彩观察,请参阅评论
如果假设值为
values = '2 #"room 2."# 5 1 -1 -1'
然后该做什么。解决方案是让字符串简单替换#“和”#to just“
import shlex
values = '2 #"room 2."# 5 1 -1 -1'
val=values.replace('#"','"')
print(shlex.split(val.replace('"#','"')))
输出
['2', 'room 2.', '5', '1', '-1', '-1']
答案 1 :(得分:0)
不使用re.split
描述分隔符,而是使用re.findall
并描述项目:
re.findall(r'(?<=")[^"]*(?=")|(?<=#)[^#]*(?=#)|[^\s"#]+', values)
答案 2 :(得分:-2)
您可以使用regexp
public void getLocation() {
try {
_locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
isGPSEnabled = _locationManager.isProviderEnabled(_locationManager.GPS_PROVIDER);
isNetworkEnabled = _locationManager.isProviderEnabled(_locationManager.NETWORK_PROVIDER);
if (_locationManager.getAllProviders().contains(LocationManager.GPS_PROVIDER))
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}_locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
if (_locationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER))
_locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
// Ask for update
Location mobileLocation = _locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (mobileLocation != null) {
onLocationChanged(mobileLocation);
}
Location netLocation = _locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (netLocation != null) {
onLocationChanged(netLocation);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onLocationChanged(Location userLocation) {
if (userLocation == null) {
Log.e(TAG, "NO Internet");
return;
}
_longitude = userLocation.getLongitude();
_latitude = userLocation.getLatitude();
if (marker != null) {
marker.remove();
}
marker = mMap.addMarker(new MarkerOptions().position(new LatLng(_latitude, _longitude)).title("I'm Here"));
String cityName = null;
Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
List<Address> addresses;
try {
addresses = gcd.getFromLocation(userLocation.getLatitude(), userLocation.getLongitude(), 1);
if (addresses.size() > 0)
cityName = addresses.get(0).getLocality();
} catch (IOException e) {
e.printStackTrace();
}
String fullLocation = "Longitude : " + userLocation.getLongitude() + "\nLatitude : " + userLocation.getLatitude() + "\n\nMy Current City is: " + cityName;
Log.e(TAG, "location : " + fullLocation);
}