正则表达式中悬挂的元字符是什么? [英] What are dangling metacharacters in regex?

查看:126
本文介绍了正则表达式中悬挂的元字符是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ruby中,我编写了一个简单的正则表达式来查找第一个{:

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

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

每当我运行此命令时,出于我的目的,所有超出该点的东西都可以正常工作,但是会出现一个轻微的错误,该错误类似于WARNING: Dangling metacharacter detected.的含义是悬挂的元字符,以及如何将正则表达式更改为明确的并尽可能高效?

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}

上述匹配模式重复了m〜n次.

Above matches PATTERN repeated m~n times.

如果要避免该警告(匹配字面上匹配{),请将其转义.

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

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

更新

BTW,/^.*{/不能捕获第一个{,因为.*是贪婪的匹配;它消耗尽可能多.

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

这篇关于正则表达式中悬挂的元字符是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆