简体   繁体   中英

Preg match a string to find all encounters of -> using regular expressions

I'm having a small technical difficulty with a regular expression, I'm trying to look at a string, let's say we have this string:

$string = "error->400";

And another string:

$string = "error->debug->warning";

As an example, I'm basically trying to do a preg_match() that returns true on any instances of -> within it.

This is my attempt but I don't understand why it doesn't work:

preg_match("/^[->]*$/", $string);

Is there a general rule for custom characters that i'm generally missing?

Thanks.

Right now, ^[->]*$ matches any number of "-" or ">" characters, from the beginning to end. You must use a group, not a character class, and anchors are not necessary. Use this to check if "->" is present in the $string :

 preg_match("/(->)/", $string);

Have a look at the example .

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