简体   繁体   中英

Regex in PHP (preg_match) pattern

I created a regex pattern that works in Dreamweaver's regex Find, but when dropped into the pattern of preg_match, it fails. What am I breaking in the PHP (5.1.6) regex rules that otherwise works in Dreamweaver's interpretation? Here's the PHP:

preg_match("/(\{a\})([a-zA-Z0-9{} .])+(\{/a\})/i", "{a}{900678}{abcde}{0}{0}{0}{/a}");

Returns false currently. How can I modify the pattern so that it matches any string that begins with {a}anything goes in the middle{/a} type strings? I realize that the above regex will not match 'anything' in the middle, but I simplified the expression for debugging.

The slash in the /a part is being interpreted as the end delimiter of the expression. You should probably use another delimiter for the whole pattern, eg:

preg_match("~(\{a\})([a-zA-Z0-9{} .])+(\{/a\})~i",
           "{a}{900678}{abcde}{0}{0}{0}{/a}");

See it in action .

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