Jump to content

[SOLVED] preg_match_all simple question...


Recommended Posts

This should be really simple, but can't work it out.

 

I have multiple values eg {username} in a string.

 

Inside the { } brackets, valid charachters are a-z (upper and lower) 0-9 and _

 

preg_match_all('/{([^}]*)}/',$var,$varmatch);		

 

Is my current code, but need to limit this to the above characters only.

What expression should I use?

Link to comment
https://forums.phpfreaks.com/topic/176833-solved-preg_match_all-simple-question/
Share on other sites

I haven't tested it but how about...

 

'~{[A-Za-z0-9_]*?}~'

 

This may also work, but unfortunately off the top of my head I'm not certain what characters \w matches, I know it's word characters, but this may well include characters you don't want like apostrophe and hyphen.

 

'~{[\w\d_]*?}~'

Iunfortunately off the top of my head I'm not certain what characters \w matches, I know it's word characters, but this may well include characters you don't want like apostrophe and hyphen.

 

'~{[\w\d_]*?}~'

 

Without going into locale issues (which have direct ramifications on what stuff like \w includes), for all intents and purposes, \w = [a-zA-Z0-9_] So in your snippet there, you have added \d and _ for nothing (now that you know what constitutes as \w (more or less / basically).

 

Just to clear up does the * return the content's between the braces?

 

No, the * is known as a 'zero or more times' quantifier (a quantifier, well, quantifies something).

So if you have a pattern like /a*b/, end results could include:

b

ab

aab

aaaaab

aaaaaaaaaaab etc...

 

You can read up on regex in the following links to help you better understand it:

 

http://www.phpfreaks.com/tutorial/regular-expressions-part1---basic-syntax

http://www.regular-expressions.info/

http://weblogtoolscollection.com/regex/regex.php

http://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1239475391&sr=8-1

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.