正则表达式可以用于此特定字符串操作? [英] Can Regex be used for this particular string manipulation?

查看:113
本文介绍了正则表达式可以用于此特定字符串操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要替换的字符(说)的 X 与字符(说) P 在一个字符串,但只有当它被包含在报价子。
的例子使得它更清晰的:

I need to replace character (say) x with character (say) P in a string, but only if it is contained in a quoted substring. An example makes it clearer:

axbx'cxdxe'fxgh'ixj'k  -> axbx'cPdPe'fxgh'iPj'k

让我们假设,为简单起见,总是引用成对出现。

Let's assume, for the sake of simplicity, that quotes always come in pairs.

最明显的方式就是一次处理(一个简单的状态机的方法)的字符串的一个字符;

然而,我想知道如果正则表达式可以用来做所有的处理一气呵成。

The obvious way is to just process the string one character at a time (a simple state machine approach);
however, I'm wondering if regular expressions can be used to do all the processing in one go.

我的目标语言是C#,但我想我的问题涉及具有内置任何语言或库。支持正则表达式

My target language is C#, but I guess my question pertains to any language having builtin or library support for regular expressions.

推荐答案

我可以使用Python做到这一点:

I was able to do this with Python:

>>> import re
>>> re.sub(r"x(?=[^']*'([^']|'[^']*')*$)", "P", "axbx'cxdxe'fxgh'ixj'k")
"axbx'cPdPe'fxgh'iPj'k"

这样做是利用非获取匹配(?= ...)检查字符x是带引号的字符串内。它寻找一些nonquo​​te字符到下一个报价,然后查找的任一单个字符或字符引述基的序列,直到字符串的结尾。

What this does is use the non-capturing match (?=...) to check that the character x is within a quoted string. It looks for some nonquote characters up to the next quote, then looks for a sequence of either single characters or quoted groups of characters, until the end of the string.

此依赖于你的假设,即引号总是平衡的。这也是不很有效。

This relies on your assumption that the quotes are always balanced. This is also not very efficient.

这篇关于正则表达式可以用于此特定字符串操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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