简体   繁体   中英

Regex how to match a word without a character in it

How can I match something like {anyWord} but exclude anything that has an underscore at the beginning, like this: {_doNotMatchThis} ?

Input:

hi there {matchPlease}{andThis}, just doing some regex {_notThis}

Desired matches:

{matchPlease}
{andThis}

Without that condition of excluding the starting {_ , I know that this regex pattern works well: \\{\\w+\\}

I've tried to modify that to be, \\{!_\\w+\\} , basically saying, "match anything that starts with { , then has a word that does not start with _ , then ends in } ". Thanks.

How about this?

/\{([^_].*?)\}/

And per @Geo and @nhahtdh in the comments, if you want only words:

/\{([^_]\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