Regex.Replace改变PHP定义值

时间:2015-06-02 17:05:15

标签: c#

我正在寻找正则表达式语法来检查一个模式并更改匹配字符串中的值。

PHP定义了如下函数:

func.apply(1,[2,3]);

但PHP也适用于这种情况

define('KEY', 'VALUE');

所以,如果我们有

define ('KEY', 'VALUE');
define    ('KEY'  , 'VALUE');
define('KEY', 'VALUE'        );
define('KEY',       'VALUE')  ;

我们如何改变关键的价值" KEY1"通过Regex.Replace?

2 个答案:

答案 0 :(得分:1)

这是如此丑陋的IMO,但它确实有效。设置正则表达式以捕获组中的所有内容,然后将要替换的组替换为要替换它的值。

群组由开放和&在它们前面没有\的紧密括号。

所以在这种情况下我们有3组:

  • 第一组捕获“define('KEY',”在有效区域之间有任意数量的空格
  • 第二组捕获您有兴趣替换的区域
  • 第3组捕获第2组之后的所有其他内容

更新

现在知道您将PHP文件的全部内容放在单个字符串变量中,请尝试以下操作:

            string testPhp = @"
Lets put some garbage in here for testing purposes
define ('KEY1', 'VALUE1');
define    ('KEY2'  , 'VALUE2');
define('KEY3', 'VALUE3'        );
define('KEY5','VALUE5');
MORE GARBAGE
MORE GARBAGE
MORE GARBAGE

define('KEY4',       'VALUE4')  ;";

            testPhp = ReplaceValueRegex(testPhp, "KEY1", "TheNewValueRegex1");
            Console.WriteLine(ReplaceValueRegex(testPhp, "KEY4", "TheNewValueRegex4"));
            Console.WriteLine();

ReplaceValueRegex()看起来像:

private static string ReplaceValueRegex(string theString, string key, string value)
{
    // This is the group you're replacing ---|
    //                                       V
    string pattern = @"(define\s*\(.*,\s*')(\w*)('\s*\)\s*;)";
    MatchCollection mc = Regex.Matches(theString, pattern);
    for (int i = 0; i < mc.Count; i++)
    {
        if (mc[i].Value.Contains(key))
        {
            string stringToReplace = mc[i].Value;
            string replacementString = Regex.Replace(stringToReplace, pattern, String.Format("$1{0}$3", value));
            theString = theString.Replace(stringToReplace, replacementString);
        }
    }
    return theString;
}

结果:

Lets put some garbage in here for testing purposes
define ('KEY1', 'TheNewValueRegex1');
define    ('KEY2'  , 'VALUE2');
define('KEY3', 'VALUE3'        );
define('KEY5','VALUE5');
MORE GARBAGE
MORE GARBAGE
MORE GARBAGE

define('KEY4',       'TheNewValueRegex4')  ;

一次与单线交易的旧答案

// This is the group that you're replacing
// --------------------------------------|
//                                       V                                                           
string pattern = @"(define\s*\(.*,\s*')(\w*)('\s*\)\s*;)";

string string1 = "define ('KEY', 'VALUE');";
string string2 = "define    ('KEY'  , 'VALUE');";
string string3 = "define('KEY', 'VALUE'        );";
string string4 = "define('KEY',       'VALUE')  ;";
string string5 = "define('KEY','KEY2','VALUE');";

Console.WriteLine(Regex.Replace(string1, pattern, "$1Hello$3"));
Console.WriteLine(Regex.Replace(string2, pattern, "$1ChangeMe$3"));
Console.WriteLine(Regex.Replace(string3, pattern, "$1GoodBye$3"));
Console.WriteLine(Regex.Replace(string4, pattern, "$1ThisWorked$3"));
Console.WriteLine(Regex.Replace(string5, pattern, "$1Action$3"));

结果:

define ('KEY', 'Hello');
define    ('KEY'  , 'ChangeMe');
define('KEY', 'GoodBye'        );
define('KEY',       'ThisWorked')  ;
define('KEY','KEY2','Action');

答案 1 :(得分:0)

这样的事情应该这样做:

telerik:RadPdfViewer x:Name="pdfEmailViewer" Grid.Row="2" DocumentSource="{Binding AttachmentPath,Converter={StaticResource DebugConverter}}"> 用于任何数量的空白&#39;

\s*