Welcome to the MetaMod and Chameleon Support Forums.

Before posting, please check out the FAQs.

helpme

 

Need extra help with your Joomla site? Consider paid Joomla support by the developer of Chameleon and MetaMod.

 

How to use Regex in a url

How to use Regex in a url

I'm new to using regex and am trying to detect a variable number between 1 & 5000 using a regex expression in a page url rule. Do I use the normal start and end delimiters ^ $ within the url? The example in the documentation is showing hash's and in one of the posts something completely different so I'm not sure the correct syntax.

Thanks for your help.

Mobilepro
Beginner Modder
ranks
useravatar
Offline
3 Posts
Administrator has disabled public posting

Re: How to use Regex in a url

Hi,

the regexes need to start and end with a delimiter. It's fairly traditional in PHP to use a forward slash (/), but since that's often found in the string that you are searching, then I prefer to use a # instead. The search pattern than has to start and end with that character. It's actually completely optional as to what character you use as per the PHP spec, but it has to be the same on both ends.

The ^ and $ (if you want to use them) go inside the end characters.

e.g. #^http://www\.mysite\.com/mypage123$#

(remembering to escape the "." characters with a backslash)

Ok, for isolating numbers between 1 and 5000 with regex, that's quite tricky. I think I'd do it this way, with alternatives for the different number of digits (1-4). This will catch the numbers 0-9, 10-99, 100-999, 1000-4999, and 5000. I've use an example of a url that ends in mypage.html?id=digits

#mypage\.html\?id=(([0-9])|([1-9][0-9])|([1-9][0-9][0-9])|([1-4][0-9][0-9][0-9])|5000)$#

Hope that helps,
Stephen

Stephen Brandon
MetaMod / Chameleon developer
If you use MetaMod or Chameleon, please post a rating and a review at the Joomla! Extensions Directory: Chameleon | MetaMod

metamodguy
useravatar
Offline
3313 Posts
User info in posts
Administrator has disabled public posting

Re: How to use Regex in a url

Hi !

How can we regex two different things in Page URL?

ex: page1 or page2
or
ex: page1 and page2

Vseva
Beginner Modder
ranks
useravatar
Offline
2 Posts
Administrator has disabled public posting

Re: How to use Regex in a url

Page1 or page2:

#^(page1|page2)\.html$#

I don't think you really want to detect page1 AND page2: the page can't be both things at once, so I think you're wanting the "OR" version above.

Cheers,
Stephen

Stephen Brandon
MetaMod / Chameleon developer
If you use MetaMod or Chameleon, please post a rating and a review at the Joomla! Extensions Directory: Chameleon | MetaMod

metamodguy
useravatar
Offline
3313 Posts
User info in posts
Administrator has disabled public posting

Board Info

Board Stats:
 
Total Topics:
1667
Total Polls:
6
Total Posts:
5910
Total Posts Today:
1
User Info:
 
Total Users:
10152
Newest User:
kasautiizindagiikay
Members Online:
0
Guests Online:
210

Online: 
There are no members online

Forum Legend:

 Topic
 New
 Locked
 Sticky
 Active
 New/Active
 New/Locked
 New Sticky
 Locked/Active
 Active/Sticky
 Sticky/Locked
 Sticky/Active/Locked