GVKun编程网logo

带标志的Python re.sub不会替换所有出现的事件(python标志性代码)

32

这篇文章主要围绕带标志的Pythonre.sub不会替换所有出现的事件和python标志性代码展开,旨在为您提供一份详细的参考资料。我们将全面介绍带标志的Pythonre.sub不会替换所有出现的事件

这篇文章主要围绕带标志的Python re.sub不会替换所有出现的事件python标志性代码展开,旨在为您提供一份详细的参考资料。我们将全面介绍带标志的Python re.sub不会替换所有出现的事件的优缺点,解答python标志性代码的相关问题,同时也会为您带来c# – 正则表达式替换所有出现次数、c# – 用单个值替换所有出现的字符串(在数组中)、JavaScript如何替换所有出现的字符串?、java正则表达式并替换所有出现的的实用方法。

本文目录一览:

带标志的Python re.sub不会替换所有出现的事件(python标志性代码)

带标志的Python re.sub不会替换所有出现的事件(python标志性代码)

Python文档说:

re.MULTILINE:指定时,模式字符’^’在字符串的开头和每行的开头(紧随每个换行符之后)匹配…默认情况下,’^’仅在字符串的开头匹配…

那么,当我得到以下意外结果时,该怎么办?

>>> import re>>> s = """// The quick brown fox.... // Jumped over the lazy dog.""">>> re.sub(''^//'', '''', s, re.MULTILINE)'' The quick brown fox.\n// Jumped over the lazy dog.''

答案1

小编典典

看一下的定义re.sub

re.sub(pattern, repl, string[, count, flags])

第四个参数是计数,你使用re.MULTILINE(是8)作为计数,而不是标志。

使用命名参数:

re.sub(''^//'', '''', s, flags=re.MULTILINE)

或先编译正则表达式:

re.sub(re.compile(''^//'', re.MULTILINE), '''', s)

c# – 正则表达式替换所有出现次数

c# – 正则表达式替换所有出现次数

参见英文答案 > My regex is matching too much. How do I make it stop?5个
我需要替换一个字符串,它遵循一个特定的字符串和一些不同的数据.我需要保持开头和中间,只需更换结束.当我尝试下面的代码时,它只替换最后一次出现.我试过切换到非贪婪的比赛,但后来却找不到它.中间可以包含新的行,空格,字母和数字.
String s = "Beginning of story. Keep this sentence. Old ending.\n";
s += s;
s += s;
s1 = Regex.Replace(s,@"Beginning of story. ([\s\S]*) Old ending.","Beginning of story. " + @"$1" + " New ending.",RegexOptions.Multiline | RegexOptions.IgnoreCase);

The result is this:
Beginning of story. Keep this sentence. Old ending.
Beginning of story. Keep this sentence. Old ending.
Beginning of story. Keep this sentence. Old ending.
Beginning of story. Keep this sentence. New ending.

如何更换“旧结局”的每一次出现.

解决方法

我认为Kendall正在使用相关链接 a non greedy match,例如
s1 = Regex.Replace(s,@"Beginning of story. ([\s\S]*?) Old ending.",RegexOptions.Multiline | RegexOptions.IgnoreCase);

应该做的伎俩.

编辑:

您还应该能够将捕获区域内的模式更改为:.*在哪里将匹配除换行符之外的任何字符.

c# – 用单个值替换所有出现的字符串(在数组中)

c# – 用单个值替换所有出现的字符串(在数组中)

我有一个字符串数组:
string[] arr2 = { "/","@","&" };

我有另一个字符串(即strValue).是否有一种干净的方法用单个值(即下划线)替换数组内容的所有实例?所以之前:

strValue = "a/ new string,with some@ values&"

之后:

strValue = "a_ new string,with some_ values_"

我考虑过这样做:

strValue = strValue.Replace("/","_");
strValue = strValue.Replace("@","_");
strValue = strValue.Replace("&","_");

但我要替换的角色数组可能会变得更大.

解决方法

你可以自己编写,而不是一遍又一遍地使用替换.这可能是你提到的性能提升

But my array may get a lot bigger.

public string Replace(string original,char replacement,params char[] replaceables)
{
    StringBuilder builder = new StringBuilder(original.Length);
    HashSet<char> replaceable = new HashSet<char>(replaceables);
    foreach(Char character in original)
    {
        if (replaceable.Contains(character))
            builder.Append(replacement);
        else
            builder.Append(character);
    }
    return builder.ToString();
}

public string Replace(string original,string replaceables)
{
    return Replace(original,replacement,replaceables.tochararray());
}

可以像这样调用:

Debug.WriteLine(Replace("a/ new string,with some@ values&",'_','/','@','&'));
Debug.WriteLine(Replace("a/ new string,new[] { '/','&' }));
Debug.WriteLine(Replace("a/ new string,existingArray));
Debug.WriteLine(Replace("a/ new string,"/@&"));

输出:

a_ new string,with some_ values_
a_ new string,with some_ values_

正如@Sebi指出的那样,这也可以作为一种扩展方法:

public static class StringExtensions
{
    public static string Replace(this string original,params char[] replaceables)
    {
        StringBuilder builder = new StringBuilder(original.Length);
        HashSet<Char> replaceable = new HashSet<char>(replaceables);
        foreach (Char character in original)
        {
            if (replaceable.Contains(character))
                builder.Append(replacement);
            else
                builder.Append(character);
        }
        return builder.ToString();
    }

    public static string Replace(this string original,string replaceables)
    {
        return Replace(original,replaceables.tochararray());
    }
}

用法:

"a/ new string,with some@ values&".Replace('_','&');
existingString.Replace('_','&' });
// etc.

JavaScript如何替换所有出现的字符串?

JavaScript如何替换所有出现的字符串?

我有这个字符串:

"Test abc test test abc test test test abc test test abc"

正在做:

str = str.replace(''abc'', '''');

似乎只删除了abc上面字符串中的第一个匹配项。

如何替换 所有 出现的内容?

答案1

小编典典

注意:请勿在对性能有要求的代码中使用此代码。

作为简单文字字符串的正则表达式的替代方法,您可以使用

str = "Test abc test test abc test...".split("abc").join("");

一般模式是

str.split(search).join(replacement)

在某些情况下,它过去比使用replaceAll和正则表达式要快,但是在现代浏览器中,情况似乎不再如此。

结论:如果您有性能至关重要的用例(例如,处理数百个字符串),请使用Regexp方法。但是对于大多数典型的用例来说,不必担心特殊字符是值得的。

java正则表达式并替换所有出现的

java正则表达式并替换所有出现的

如何解决java正则表达式并替换所有出现的?

我想替换一个大字符串中的一个字符串,但我猜我的正则表达式不正确。所以它不起作用。

主字符串是

一些要替换的sql部分

cond =  emp.EMAIL_ID = ''xx@xx.com'' AND
emp.PERMANENT_ADDR LIKE(''%98n%'') 
AND hemp.EMPLOYEE_NAME = ''xxx'' and is_active=''Y''

要查找和替换的字符串是

根据某些条件替换sql部分

hemp.EMPLOYEE_NAME = ''xxx''

我已经尝试过

使用模式和匹配器类

Pattern pat1 = Pattern.compile("/^hemp.EMPLOYEE_NAME\\s=\\s\''\\w\''\\s[and|or]*/$",Pattern.CASE_INSENSITIVE);
        
        Matcher mat = pat1.matcher(cond);
         while (mat.find()) {
                     System.out.println("Match: " + mat.group());
                    cond = mat.replaceFirst("xx "+mat.group()+"x");
                    mat = pat1.matcher(cond);
        
        }

它不起作用,根本没有进入循环。任何帮助表示赞赏。

解决方法

显然不是 - 您的正则表达式模式没有任何意义。

  • 开头的 /:在某些语言中,正则表达式不是字符串并以开头斜杠开头。 Java 不是这些语言之一,它与正则表达式本身无关。因此,这会在该 SQL 中查找字面斜杠,但该斜杠不存在,因此失败。
  • ^ 是“字符串开头”的正则表达式。您的字符串不以 hemp.EMPLOYEE_NAME 开头,因此也不起作用。在这里去掉 / 和 ^。
  • \\s 是一个空白字符(有许多空白字符——这匹配其中的任何一个,尽管只有一个)。您的字符串没有任何空格。当然,您的意图是 \\s* 匹配 0 到其中许多,即:\\s* 是:“此处允许使用空格”。 \\s 是:此处必须只有一个空格字符。将正则表达式中的所有 \\s 设为 \\s*
  • \\w 正好是一个“单词”字符(或多或少是一个字母或数字),您显然需要 \\w*
  • [and|or] 这是正则表达式:“一个 a,或一个 n,或一个 d,或一个 o,或一个 r,或一个管道符号”。显然,您正在寻找 (and|or),它是正则表达式:序列“and”或序列“or”。
  • * - 所以你想要 0 到多个“和”或“或”,这是没有意义的。
  • 结束斜线:你不想要这个。
  • closure $:你不想要这个 - 这意味着“字符串结束”。您的字符串并未到此结束。

代码本身:

replaceFirst 本身也做正则表达式。你不想双重应用这些东西。这不是替换找到的结果的方式。

这就是你想要的:

Matcher mat = pat1.matcher(cond);
mat.replaceFirst("replacement goes here");

如果您想获取匹配内容的一部分(即不要使用 mat.group(),使用这些引用),则替换可以包括对匹配中组的引用。

更一般地说,您是否阅读过任何正则表达式教程、进行过任何测试或阅读过 Pattern 和 Matcher 的 javadoc?

我已经开发了几年。也许这只是个人经验,但是,阅读是非常基础的。

,

代替锚点 ^$,您可以使用单词边界 \b 来防止部分匹配。

如果要匹配同一行的空格,可以使用\h来匹配水平空白字符,因为\s也可以匹配换行符。

您可以在字符串上使用 replaceFirst 使用 $0 获取完整匹配,并使用内联修饰符 (?i) 获取不区分大小写的匹配。

注意,使用 [and|or] 是匹配列出的字符之一的 character class 并转义点以匹配它的字面意思,否则 . 匹配任何字符除了换行符。

(?i)\bhemp\.EMPLOYEE_NAME\h*=\h*''\w+''\h+(?:and|or)\b

查看 regex demo 或 Java demo

例如

String regex = "\\bhemp\\.EMPLOYEE_NAME\\h*=\\h*''\\w+''\\h+(?:and|or)\\b";
String string = "cond =  emp.EMAIL_ID = ''xx@xx.com'' AND\n"
+ "emp.PERMANENT_ADDR LIKE(''%98n%'') \n"
+ "AND hemp.EMPLOYEE_NAME = ''xxx'' and is_active=''Y''";

System.out.println(string.replaceFirst(regex,"xx$0x"));

输出

cond =  emp.EMAIL_ID = ''xx@xx.com'' AND
emp.PERMANENT_ADDR LIKE(''%98n%'') 
AND xxhemp.EMPLOYEE_NAME = ''xxx'' andx is_active=''Y''

今天的关于带标志的Python re.sub不会替换所有出现的事件python标志性代码的分享已经结束,谢谢您的关注,如果想了解更多关于c# – 正则表达式替换所有出现次数、c# – 用单个值替换所有出现的字符串(在数组中)、JavaScript如何替换所有出现的字符串?、java正则表达式并替换所有出现的的相关知识,请在本站进行查询。

本文标签: