-2

我想使用 Regex.Replace 方法。我将在很长的字符串中搜索“new long?((long)”表达式并进行以下更改。

前:

OracleParameter[] array = new OracleParameter[]
{
    new OracleParameter("p_op_adi", OracleDbType.Varchar2, p_op_adi, ParameterDirection.Input),
    new OracleParameter("p_tip_no", OracleDbType.Int64, p_tip_no, ParameterDirection.Input),
    new OracleParameter("p_islem", OracleDbType.Varchar2, p_islem, ParameterDirection.Input),
    new OracleParameter("p_raise", OracleDbType.Varchar2, p_raise, ParameterDirection.Input),
    new OracleParameter("p_durum", OracleDbType.Int64, null, ParameterDirection.Output),
    new OracleParameter("p_mesaj", OracleDbType.Varchar2, 4000, null, ParameterDirection.Output)
};
sMan.ExecuteSP("FRM_USER.user_yetki_kontrol_islem", array);
p_durum = ((array[4].Value != System.DBNull.Value) ? new long?((long)array[4].Value) : null);
p_mesaj = array[5].Value.ToString();

正则表达式之后:

OracleParameter[] array = new OracleParameter[]
{
    new OracleParameter("p_op_adi", OracleDbType.Varchar2, p_op_adi, ParameterDirection.Input),
    new OracleParameter("p_tip_no", OracleDbType.Int64, p_tip_no, ParameterDirection.Input),
    new OracleParameter("p_islem", OracleDbType.Varchar2, p_islem, ParameterDirection.Input),
    new OracleParameter("p_raise", OracleDbType.Varchar2, p_raise, ParameterDirection.Input),
    new OracleParameter("p_durum", OracleDbType.Int64, null, ParameterDirection.Output),
    new OracleParameter("p_mesaj", OracleDbType.Varchar2, 4000, null, ParameterDirection.Output)
};
sMan.ExecuteSP("FRM_USER.user_yetki_kontrol_islem", array);
p_durum = abcde(array[4].Value);
p_mesaj = array[5].Value.ToString();

请注意,我必须捕获数组索引号 4。

4

2 回答 2

1

此代码应该完全符合您的要求:

class Program
{
    static void Main(string[] args)
    {
        string input = @"OracleParameter[] array = new OracleParameter[]
            {
                new OracleParameter('p_op_adi', OracleDbType.Varchar2, p_op_adi, ParameterDirection.Input),
                p_durum = ((array[4].Value != System.DBNull.Value) ? new long?((long)array[4].Value) : null);
                p_durum = ((array[4].Value != System.DBNull.Value) ? new long?((long)array[4].Value) : null);
            ";

        string pattern = @"^(?<varname>[^=]*)=.*new long\?\(\(long\)(?<arrayval>[^)]*).*?$";
        string replacement = "${varname} = abcde(${arrayval});";

        string output = Regex.Replace(input, pattern, replacement, RegexOptions.Multiline);
        Console.WriteLine(output);
        Console.ReadKey();
    }
}

它获取变量名称和数组值,并将它们用作替换部分中的替换。

于 2012-08-10T14:58:11.600 回答
0

这个正则表达式选择完整的字符串(?m)^.*?new long\?\(\(long\).*?$

string res = Regex.Replace(input, @"(?m)^.*?new long\?\(\(long\).*?$", "p_durum = abcde(array[4].Value);");
于 2012-08-10T14:44:26.650 回答