tags:

views:

34

answers:

2

hi all, 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.

+2  A: 

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.

David Zaslavsky
+1  A: 

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

Related questions

polygenelubricants
Don't forget the anchors, or perhaps `\b` word boundaries.
polygenelubricants