Regex help! |
[eluser]Ryuuzaki92[/eluser]
hi, i want a regexp to detect is_admin => admin is_admin_or_lower => admin, lower is_admin_or_higher => admin, higher im currently using this but it doesnt detect the _or_lower|higher Code: /^is_(.+)(_or_(higher|lower))?$/
[eluser]xwero[/eluser]
have you tried Code: /^is_(.+)(_or_higher|_or_lower)?$/
[eluser]Ryuuzaki92[/eluser]
thanks guys. this works: Code: '/^is_([a-z]+)(_or_(higher|lower))?$/'
[eluser]xwero[/eluser]
Doosje the underscore isn't a metacharacter AFAIK so i don't think that is the problem. I tested the regex and the problem is with the (.+) it catches admin_or_higher. So the regex will have to be something like Code: /^is_([a-zA-Z]+)_or_?(higher|lower)?$/ - admin -_or_higher - higher edit : it's monday for all of us ![]()
[eluser]Doosje[/eluser]
xwero, you're right .. but .. i make a llittle adustment .. bucase _or_ alsso needs to be in catched Code: /^is_([a-zA-Z]+)(_or_(higher|lower))?$/ (at least, if i'm reading right but it's early for me .. only awake for half an hour)
[eluser]m4rw3r[/eluser]
Try making (.+) to a non greedy expression: (.+?) Then you don't have to restrict the characters in the username
[eluser]Ryuuzaki92[/eluser]
[quote author="m4rw3r" date="1206484114"]Try making (.+) to a non greedy expression: (.+?) Then you don't have to restrict the characters in the username[/quote] oo great tip! but can you explain to me why is it different? im really a noob @ regexp =(
[eluser]xwero[/eluser]
the question mark is used for string that can be there or not like Code: /_or_?(higher|lower)?/ Another good to know option with the question mark is ?: before a group like Code: /(?:[a-z]*)_(higher|lower)/ If you want a site with information about regex go to http://www.regular-expressions.info/ |
Welcome Guest, Not a member yet? Register Sign In |