Jump to content

[SOLVED] help with a preg_replace expression


Renlok

Recommended Posts

well if i input a string which contains {TEST(0)} for example i wont it so the TEST is \\1 and the 0 is \\2 in the replace string ive tried this with this expression but it doesnt work and im not sure why

 

'#\{([a-z0-9\-_]+[\(0-9\)]+)\}#is'

 

thanks in advance for any help

Link to comment
Share on other sites

One suggestion could be:

 

$str = '{TEST(0)}';
preg_match('#{([a-z0-9_-]+)\(([0-9]+)\)}#i', $str, $matches);
// $matches[1] a.k.a = TEST
// $matches[2] a.k.a = 0

Note that in your pattern, you can put the dash that you have in the first character class as first or last listed and thus won't need escaping. Also note some issues with your second half of the pattern. You are using a character class to look for either a ( [which inside a character class doesn't need escaping], or 0-9+ or a closing ). Note that all characters inside [..] only checks for a single character, not a sequence of characters.

 

EDIT - also note that the s modifier in your example won't do anything, as that only applies to the dot_match_all wildcard character in conjunction with it matching newlines.

Link to comment
Share on other sites

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.