开发者

Regex validity for Degree

i have an assignment where i should开发者_如何转开发 check validity of the string where :

  • N90 -> North and must have key degree between 0-90
  • W180 -> have degree between 91-180
  • S270 -> have degree between 181-270
  • E360 -> have degree between 271-360

how can i create a reg ex like this.


Don't use a regular expression. This is a classic example of the kind of problem that regular expressions are not appropriate for. Just split the string into a direction (the first character) and a number, and check that the number meets the required conditions for the direction.


Regex doesn't really understand numeric ranges, and must match these numbers as digit sequences, so it's not the best tool for the job. That said, this can be done, but it's going to be ugly

N90 -> North and must have key degree between 0-90

N([0-9]|[1-8][0-9]|90)

W180 -> have degree between 91-180

W(9[1-9]|1[0-7][0-9]|180)

S270 -> have degree between 181-270

S(18[1-9]|19[0-9]|2[0-6][0-9]|270)

E360 -> have degree between 271-360

E(27[1-9]|2[8-9][0-9]|3[0-5][0-9]|360)

References

  • regular-expressions.info/Numeric Ranges

Related questions

  • Regex: why doesn’t [01-12] range work as expected?
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜