普通防爆pression找到一个字符串包含两个字符之间,而剔除分隔符 [英] Regular Expression to find a string included between two characters while EXCLUDING the delimiters

查看:130
本文介绍了普通防爆pression找到一个字符串包含两个字符之间,而剔除分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从字符串中提取一组包括两个分隔符之间的字符,而不返回分隔符本身。

I need to extract from a string a set of characters which are included between two delimiters, without returning the delimiters themselves.

一个简单的例子应该是有帮助的:

A simple example should be helpful:

目标:提取方括号中的字符串,而无需返回括号本身

Target: extract the substring between square brackets, without returning the brackets themselves.

基本字符串这是一个测试字符串[或多或少]

如果我用下面的章。恩。

If I use the following reg. ex.

\[.*?\]

本场比赛是的 [或多或少] 。我需要得到唯一的更多或更少(不带支架)。

The match is [more or less]. I need to get only more or less (without the brackets).

是否有可能呢?

推荐答案

易做的:

(?<=\[)(.*?)(?=\])

这是一个使用向前看符号和lookbehinds技术上。请参见前瞻和后向零宽断言。该模式包括:

Technically that's using lookaheads and lookbehinds. See Lookahead and Lookbehind Zero-Width Assertions. The pattern consists of:


  • 是由[未捕获(后向)pceded $ P $;

  • 一个非贪婪捕获组。它的非贪婪停在第一]和

  • 后面是没有拍摄的(前瞻)。

另外,你可以只捕捉方括号什么:

Alternatively you can just capture what's between the square brackets:

\[(.*?)\]

和返回第一个捕获组,而不是整场比赛的。

and return the first captured group instead of the entire match.

这篇关于普通防爆pression找到一个字符串包含两个字符之间,而剔除分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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