正则表达式来选择匹配的引号之间的数据 [英] Regex pattern to choose data BETWEEN matching quotation marks

查看:165
本文介绍了正则表达式来选择匹配的引号之间的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假如我有以下字符串我想运行一个正则表达式:

Suppose I had the following string I wanted to run a Regular expression on:

This is a test string with "quotation-marks" within it.
The "problem" I am having, per-se, is "knowing" which "quotation-marks"
go with which words.

现在,假设我想替换所有 - 字符引号有,比如说,一个空间的。我想我可以用正则表达式寻找这样做如下:

Now, suppose I wanted to replace all the - characters between the quotation marks with, say, a space. I was thinking I could do so with a regex looking as follows:

Find What:      (\"[^"]*?)(\-)([^"]*?\")
Replace With:   $1 $3

我遇到的问题是,使用这种模式,它没有考虑到一个引号是否被打开或关闭的声明。

The problem I'm having is that using this pattern, it does not take into account whether a quotation mark was opening or closing the statement.

因此,在上面的例子中, - 字符每本身将被替换成空格,因为它是之间2引号,但关闭和打开标记之间 - 当我特别希望在文本中查找一个开幕式和闭幕标记

So, in the example above, the - character in per-se will be replaced by a space since it is between 2 quotation marks, but between a closing and an opening mark - When I specifically want to look within the text between an opening and a closing mark.

你如何在这样一个正则表达式解释呢?

How do you account for this in such a regular expression?

我希望这是有道理的。

我用VB / C#正则表达式。

I'm using VB / C# Regex.

刚刚完成的问题(并希望阐述更多,如果必要的一个位),最后的结果我想获得将是:

Just to complete the question (and hopefully elaborate a bit more if necessary), the end result I would like to get would be:

This is a test string with "quotation marks" within it.
The "problem" I am having, per-se, is "knowing" which "quotation marks"
go with which words.



谢谢!

Thanks!!

推荐答案

猛击我的大脑来解决这个一出来,原来,指定的非字边界 \B 的伎俩

Busted my brain to work this one out, turns out that specifying non-word boundaries \B does the trick:

\B([^] *) - ([^] *)\B

$ 1 $ 2

http://regex101.com/r/dS0bH8

这篇关于正则表达式来选择匹配的引号之间的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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