简体   繁体   中英

Creating Regular Expression to search a string

I have a string that contains variables in the format {namespace:name} I am trying to create a regular expression for finding all of the variables in the string. I have the following so far, but it isn't working:

$str = " {user:fname} and the last name = {user:lname}";
var_dump(preg_match_all("/^\{(\w):(\w)\}/", $str, $matches));   
var_dump($matches);

But it isn't finding any of the tags. The variables can have any word for namespace and name, but letters only with no spaces. Any help would be appreciated.

Update

I tried the following also and received no results: "/\\{(\\w):(\\w)\\}/"

Remove the anchor ^ from the regex and allow variables with a length of more than one character.

/^\{(\w):(\w)\}/

becomes:

/\{(\w+):(\w+)\}/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM