简体   繁体   中英

What are dangling metacharacters in regex?

In Ruby, I wrote a simple regex to find the first { :

txt.gsub! /^.*{/, '{'

Whenever I run this, everything past that point for my purposes works fine, however there is a mild error that reads along the lines of WARNING: Dangling metacharacter detected. What specifically are dangling metacharacters, and how would I change my regex to be as explicit and efficient as possible?

{ has special meaning in regular expression.

PATTERN{m,n}

Above matches PATTERN repeated m~n times.

If you want avoid that warning (to match literally match { ) escape it.

txt.gsub! /^.*\{/, '{'

UPDATE

BTW, /^.*{/ does not catch the first { because .* is greedy match; It consume as much as possible.

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