Reputation: 21
The pattern i need to check is "1w 1d 1h 1m 1s", with some restrictions, for example there can be only 1 to 3 numbers in each section, any section can be missing, there can be any numbers or spaces and other white space characters between the parts, the parts must be in order. I wrote a basic regex for it:
^((?:\s*\d{1,3}[wW]\s*)?(?:\s+\d{1,3}[dD]\s*)?(?:\s+\d{1,3}[hH]\s*)?(?:\s+\d{1,3}[mM]\s*)?(?:\s+\d{1,3}[sS]\s*)?)$
Now the problem - I got stuck, because if I write "1d 1h" - it wont pass, because it requires a space before "1d", but I cant not have that space, "1w1d" should be incorrect. Using regex conditions is not really an option.
Upvotes: 1
Views: 209
Reputation: 377
^((?:\s*\d{1,3}[wW]\s+|\s*\d{1,3}[wW]\s*$)?(?:\s*\d{1,3}[dD]\s+|\s*\d{1,3}[dD]\s*$)?(?:\s*\d{1,3}[hH]\s+|\s*\d{1,3}[hH]\s*$)?(?:\s*\d{1,3}[mM]\s+)?(?:\s*\d{1,3}[sS]\s*|\s*\d{1,3}[sS]\s*$)?)$
Upvotes: 1
Reputation: 1675
In addition to what @SamjithDasan have correctly provided, you may also utilize the regular expression conditionals supported in ActionScript-3, if it was an option:
var regex: RegExp =
/^(\s*\d{1,3}[wW]\s*(?(?=.)\s+|))?(\d{1,3}[dD]\s*(?(?=.)\s+|))?(\d{1,3}[hH]\s*(?(?=.)\s+|))?(\d{1,3}[mM]\s*(?(?=.)\s+|))?(\d{1,3}[sS]\s*)?$/g;
var i:uint = 1;
for each(var w: String in ["1w ", ""]) {
for each(var d: String in ["1d ", ""]) {
for each(var h: String in ["1h ", ""]) {
for each(var m: String in ["1m ", ""]) {
for each(var s: String in ["1s ", ""]) {
var str:String = w+d+h+m+s;
var desc:String = strFill(i++ + ". str=\""+str+"\"");
trace(desc+" RegEx-matched: "+(str.match(regex).length == 1 ? "yes" : "no"));
}
}
}
}
}
function strFill(str:String, w:uint = 25, fill:String="."):String {
for (var i:uint=str.length; i<=w; i++) str+=fill; return str;
}
And the output is:
1. str="1w 1d 1h 1m 1s ".. RegEx-matched: yes
2. str="1w 1d 1h 1m "..... RegEx-matched: yes
3. str="1w 1d 1h 1s "..... RegEx-matched: yes
4. str="1w 1d 1h "........ RegEx-matched: yes
5. str="1w 1d 1m 1s "..... RegEx-matched: yes
6. str="1w 1d 1m "........ RegEx-matched: yes
7. str="1w 1d 1s "........ RegEx-matched: yes
8. str="1w 1d "........... RegEx-matched: yes
9. str="1w 1h 1m 1s "..... RegEx-matched: yes
10. str="1w 1h 1m "....... RegEx-matched: yes
11. str="1w 1h 1s "....... RegEx-matched: yes
12. str="1w 1h ".......... RegEx-matched: yes
13. str="1w 1m 1s "....... RegEx-matched: yes
14. str="1w 1m ".......... RegEx-matched: yes
15. str="1w 1s ".......... RegEx-matched: yes
16. str="1w "............. RegEx-matched: yes
17. str="1d 1h 1m 1s ".... RegEx-matched: yes
18. str="1d 1h 1m "....... RegEx-matched: yes
19. str="1d 1h 1s "....... RegEx-matched: yes
20. str="1d 1h ".......... RegEx-matched: yes
21. str="1d 1m 1s "....... RegEx-matched: yes
22. str="1d 1m ".......... RegEx-matched: yes
23. str="1d 1s ".......... RegEx-matched: yes
24. str="1d "............. RegEx-matched: yes
25. str="1h 1m 1s "....... RegEx-matched: yes
26. str="1h 1m ".......... RegEx-matched: yes
27. str="1h 1s ".......... RegEx-matched: yes
28. str="1h "............. RegEx-matched: yes
29. str="1m 1s ".......... RegEx-matched: yes
30. str="1m "............. RegEx-matched: yes
31. str="1s "............. RegEx-matched: yes
32. str=""................ RegEx-matched: no
Upvotes: 1