例子: 这只是一个简单的句子。

我想匹配这句话和句子之间的每个字符。换行符应该被忽略。我想不出正确的语法。


当前回答

我在这里搜索regex来转换这个打印语法,在Python2中的旧脚本中使用:print("string")在Python3中打印"string"。工作得很好,否则使用2to3.py进行其他转换。以下是我对其他人的解决方案:

试试Regexr.com(不工作在NP++出于某种原因):

find:     (?<=print)( ')(.*)(')
replace: ('$2')

变量:

(?<=print)( )(.*)(\n)
('$2')\n

对于标签和变量:

(?<=print)( ')(.*)(',)(.*)(\n)
('$2',$4)\n

如何替换所有打印“字符串”在Python2打印(“字符串”)为Python3?

其他回答

有一种方法来处理在文本块中这种分裂的重复实例?例如:“这只是一个简单的句子。这里还有一些额外的东西。这只是一个简单的句子。这里还有一些东西。这只是一个简单的句子。”。要匹配每个实例而不是整个字符串,使用下面的代码:

data = "This is just\na simple sentence. Here is some additional stuff. This is just\na simple sentence. And here is some more stuff. This is just\na simple sentence."

pattern = re.compile('This is (?s).*? sentence')

for match_instance in re.finditer(pattern, data):
    do_something(match_instance.group())

以下是我的做法: 这对我来说比试图找出特定的必要正则表达式更容易。

int indexPictureData = result.IndexOf("-PictureData:");
int indexIdentity = result.IndexOf("-Identity:");
string returnValue = result.Remove(indexPictureData + 13);
returnValue = returnValue + " [bytecoderemoved] " + result.Remove(0, indexIdentity); ` 

RegEx使用Java方法匹配两个字符串之间的所有内容。

List<String> results = new ArrayList<>(); //For storing results
String example = "Code will save the world";

让我们使用Pattern和Matcher对象来使用RegEx(.?)*。

Pattern p = Pattern.compile("Code "(.*?)" world");   //java.util.regex.Pattern;
Matcher m = p.matcher(example);                      //java.util.regex.Matcher;

由于Matcher可能包含多个匹配项,我们需要遍历结果并存储它。

while(m.find()){   //Loop through all matches
   results.add(m.group()); //Get value and store in collection.
}

这个例子将只包含“will save the”这个单词,但是在更大的文本中,它可能会找到更多匹配项。

对于JavaScript,你可以使用[^]来匹配任何字符,包括换行符。

使用带点的/s标志。匹配任何字符也可以,但应用于整个模式,JavaScript不支持内联修饰符来打开/关闭标志。

为了匹配尽可能少的字符,可以通过添加问号使量词非贪婪,并使用捕获组提取介于两者之间的部分。

This is([^]*?)sentence

请参阅regex101演示。

作为旁注,为了不匹配部分单词,你可以使用像\bThis和句子\b这样的单词边界

const s = "这只是一个简单的句子"; const regex = /这是([^]*?)句子/; Const m = s.match(正则表达式); 如果(m) { console.log (m [1]); }


JavaScript中的巡视变体是(?<=This is)[^]*?(?=sentence),你可以在JS正则表达式中检查Lookbehind的支持。

请参见关于向后查找的重要说明。

const s = "这只是一个简单的句子"; 常量regex = /(?< =这是)[^]* ?(? =句子)/; Const m = s.match(正则表达式); 如果(m) { console.log (m [0]); }

Sublime Text 3x

在崇高的文本中,你只需写下你感兴趣的两个单词,例如在你的例子中,它是

"This is"和"sentence"

在中间写。*

这是*句

这对你有好处