위도/경도 좌표를 일치시키기 위한 정규식을 만들려고 합니다. 배정밀도 숫자를 일치시키기 위해 (\-?\d+(\.\d+)?)
를 사용하고 이를 단일 표현식으로 결합하려고 시도했습니다:
^(\-?\d+(\.\d+)?),\w*(\-?\d+(\.\d+)?)$
이중, 쉼표, 공백 및 다른 이중과 일치할 것으로 예상했지만 작동하지 않는 것 같습니다. 특히 공백이 하나 이상이 아닌 공백이 없는 경우에만 작동합니다. 제가 뭘 잘못했나요?
이 범위 내에서 엄격하게 일치시킵니다 해당하는 정확한 위도와 경도 값을 한 것이다.
^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$
일치시킵니다
-, - 127.554334 +90.0
90., - - - 180. -, - 100.111 +90.1 91, 123.456 - -
이러한 수준들과 (소수점 형식, 소수점 숫자 6 개) 을 사용하고 있습니다.
^(\+|-)?(?:90(?:(?:\.0{1,6})?)|(?:[0-9]|[1-8][0-9])(?:(?:\.[0-9]{1,6})?))$
^(\+|-)?(?:180(?:(?:\.0{1,6})?)|(?:[0-9]|[1-9][0-9]|1[0-7][0-9])(?:(?:\.[0-9]{1,6})?))$
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import lombok.extern.slf4j.Slf4j;
import org.testng.annotations.Test;
@Slf4j
public class LatLongValidationTest {
protected static final String LATITUDE_PATTERN="^(\\+|-)?(?:90(?:(?:\\.0{1,6})?)|(?:[0-9]|[1-8][0-9])(?:(?:\\.[0-9]{1,6})?))$";
protected static final String LONGITUDE_PATTERN="^(\\+|-)?(?:180(?:(?:\\.0{1,6})?)|(?:[0-9]|[1-9][0-9]|1[0-7][0-9])(?:(?:\\.[0-9]{1,6})?))$";
@Test
public void latitudeTest(){
DecimalFormat df = new DecimalFormat("#.######");
df.setRoundingMode(RoundingMode.UP);
double step = 0.01;
Double latitudeToTest = -90.0;
while(latitudeToTest <= 90.0){
boolean result = df.format(latitudeToTest).matches(LATITUDE_PATTERN);
log.info("Latitude: tested {}. Result (matches regex): {}", df.format(latitudeToTest), result);
assertThat(result, is(true));
latitudeToTest += step;
}
latitudeToTest = -90.1;
while(latitudeToTest >= -200.0){
boolean result = df.format(latitudeToTest).matches(LATITUDE_PATTERN);
log.info("Latitude: tested {}. Result (matches regex): {}", df.format(latitudeToTest), result);
assertThat(result, is(false));
latitudeToTest -= step;
}
latitudeToTest = 90.01;
while(latitudeToTest <= 200.0){
boolean result = df.format(latitudeToTest).matches(LATITUDE_PATTERN);
log.info("Latitude: tested {}. Result (matches regex): {}", df.format(latitudeToTest), result);
assertThat(result, is(false));
latitudeToTest += step;
}
}
@Test
public void longitudeTest(){
DecimalFormat df = new DecimalFormat("#.######");
df.setRoundingMode(RoundingMode.UP);
double step = 0.01;
Double longitudeToTest = -180.0;
while(longitudeToTest <= 180.0){
boolean result = df.format(longitudeToTest).matches(LONGITUDE_PATTERN);
log.info("Longitude: tested {}. Result (matches regex): {}", df.format(longitudeToTest), result);
assertThat(result, is(true));
longitudeToTest += step;
}
longitudeToTest = -180.01;
while(longitudeToTest >= -300.0){
boolean result = df.format(longitudeToTest).matches(LONGITUDE_PATTERN);
log.info("Longitude: tested {}. Result (matches regex): {}", df.format(longitudeToTest), result);
assertThat(result, is(false));
longitudeToTest -= step;
}
longitudeToTest = 180.01;
while(longitudeToTest <= 300.0){
boolean result = df.format(longitudeToTest).matches(LONGITUDE_PATTERN);
log.info("Longitude: tested {}. Result (matches regex): {}", df.format(longitudeToTest), result);
assertThat(result, is(false));
longitudeToTest += step;
}
}
}
악셀, regex 는 실제로 앨릭스 그들위에 잘못 latitudetm, 경도 범위의 관점.
경도 측정 범위는 180& # 176. # 를 +180& 176.
따라서 아래와 같이 주어져 보다 정확하게 검증하는 regex. 또한 아무도 내 돌아보 소수점 시점으로든지 latitudetm / 경도를 따라 제한해야 합니다.
^([-+]?\d{1,2}([.]\d+)?),\s*([-+]?\d{1,3}([.]\d+)?)$
C 충족하거나 목표
^([-+]?\\d{1,2}([.]\\d+)?),\\s*([-+]?\\d{1,3}([.]\\d+)?)$
^-?[0-9]{1,3}(?:\.[0-9]{1,10})?$
Regex 구조:
^-?[0-9]{1,3}(?:\.[0-9]{1,10})?$
'-' # 동의하십시오 음수 값을?
' [0-9] {1.3}' # 해당되어서는 1-3 숫자 (i. e. 0 999)
' (:' # 종료기 일치시키려면.
소수점 '#'.
' [0-9]} {1.10 10 자리) 에 이어' # 1 (i. e. 0-9999999999)
이거 드세요.
^(\()([-+]?)([\d]{1,2})(((\.)(\d+)(,)))(\s*)(([-+]?)([\d]{1,3})((\.)(\d+))?(\)))$
체크아웃합니다 http://support.
이와 같은 것을 맨 위 상자에 붙여넣습니다 표현식에서는 어졌다면 동일팔레트에 상자:
(80.0123, -34.034)
(80.0123)
(80.a)
(980.13, 40)
(99.000, 122.000)
Regex 구조:
^ # The string must start this way (there can't be anything before).
(\() # An opening parentheses (escaped with a backslash).
([-+]?) # An optional minus, or an optional plus.
([\d]{1,2}) # 1 or 2 digits (0-9).
( # Start of a sub-pattern.
( # Start of a sub-pattern.
(\.) # A dot (escaped with a backslash).
(\d+) # One or more digits (0-9).
(,) # A comma.
) # End of a sub-pattern.
) # End of a sub-pattern.
(\s*) # Zero or more spaces.
( # Start of a sub-pattern.
([-+]?) # An optional minus, or an optional plus.
([\d]{1,3}) # 1 to 3 digits (0-9).
( # Start of a pattern.
(\.) # A dot (escaped with a backslash).
(\d+) # One or more digits (0-9).
)? # End of an optional pattern.
(\)) # A closing parenthesis (escaped with a backkslash).
) # End of a pattern
$ # The string must end this way (there can't be anything after).
이제 할 일은 자신을 제한, 어떤 것은 아니며 이 범위:
(-90 to +90, and -180 to +180)
그 대신 간단한 제한 자체가 이 범위:
(-99 to +99, -199 to +199)
근데 요점은 바로 break down 각 부분을 주로 표현식에서는.
파이썬.
Latitudetm: '결과 = 레지마치 (". ^ [+ -]? ((90.? 0을 $) (([8 0]? [0-9]). " [0-9] $)), # 39, & # 39 " 90.00001&; -;)'
경도: '결과 = 레지마치 (". ^ [+ -]? ((180.? 0을 $) (((1 [0-7] [0-9]) ([0-9] {0.2}). " [0-9] $)), # 39, & # 39 " 0.0000&; -;)'
예를 들어, latitudetm 장애가.
@macro-ferrari I did it), 이것은 미리 견지에서 볼 수 있는 방법을 찾아야 줄여집니다 없이 모든 최근 이야기하고 [정규 표현식 엔진을 사용하는] [2]
const LAT_RE = /^[+-]?(([1-8]?[0-9])(\.[0-9]{1,6})?|90(\.0{1,6})?)$/;
const LONG_RE = /^[+-]?((([1-9]?[0-9]|1[0-7][0-9])(\.[0-9]{1,6})?)|180(\.0{1,6})?)$/;
[2]: https://swtch.com/ ~ rsc/regexp/regexp1.html
간단한 방법으로 완전하고 정확한 위도와 경도 대한 객관적인 캜 체크 패턴 입니다.
-( BOOL )textIsValidValue:(NSString*) searchedString
{
NSRange searchedRange = NSMakeRange(0, [searchedString length]);
NSError *error = nil;
NSString *pattern = @"^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?),\\s*[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)$";
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern: pattern options:0 error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:searchedString options:0 range: searchedRange];
return match ? YES : NO;
}
여기서 는 입력입니다 시시드스타링 해당 사용자가 해당 textfield 입력하십시오. 있는 것이다.
다음은 PHP& # 39 의 버전 (입력 값을 다음과 같습니다. '$ $ latitudetm' 와 '경도').
$latitude_pattern = '/\A[+-]?(?:90(?:\.0{1,18})?|\d(?(?<=9)|\d?)\.\d{1,18})\z/x';
$longitude_pattern = '/\A[+-]?(?:180(?:\.0{1,18})?|(?:1[0-7]\d|\d{1,2})\.\d{1,18})\z/x';
if (preg_match($latitude_pattern, $latitude) && preg_match($longitude_pattern, $longitude)) {
// Valid coordinates.
}
이 문제를 해결할 수 있습니다.
var latExp = /^(?=.)-?((8[0-5]?)|([0-7]?[0-9]))?(?:\.[0-9]{1,20})?$/;
var lngExp = /^(?=.)-?((0?[8-9][0-9])|180|([0-1]?[0-7]?[0-9]))?(?:\.[0-9]{1,20})?$/;
이거 드세요.
^[-+]?(([0-8]\\d|\\d)(\\.\\d+)?|90(\\.0+)?)$,\s*^[-+]?((1[0-7]\\d(\\.\\d+)?)|(180(\\.0+)?)|(\\d\\d(\\.\\d+)?)|(\\d(\\.\\d+)?))$
이거 드세요.
(?<!\d)([-+]?(?:[1-8]?\d(?:\.\d+)?|90(?:\.0+)?)),\s*([-+]?(?:180(?:\.0+)?|(?:(?:1[0-7]\d)|(?:[1-9]?\d))(?:\.\d+)?))(?!\d)`