简体   繁体   English

正则表达式要在C#中删除此字符串?

[英]Regex to remove this string in C#?

I am scripting Agent Jobs using SMO for SQL Server and the resulting script strings have a have parameter and value that I want to remove from the final version I am storing. 我正在使用SMO for SQL Server编写代理作业脚本,并且生成的脚本字符串具有我要从存储的最终版本中删除的具有参数和值。 The portion of the script that I want to look at is the schedule being added to the job, where it includes a @schedule_uid parameter with a GUID associated with it. 我要查看的脚本部分是计划添加到作业中,其中包括一个@schedule_uid参数以及与之关联的GUID。 I'd like to remove this entirely from the script. 我想从脚本中完全删除它。

EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name='Job Name', 
        @enabled=1, 
        @freq_type=4, 
        @freq_interval=1, 
        @freq_subday_type=4, 
        @freq_subday_interval=10, 
        @freq_relative_interval=1, 
        @freq_recurrence_factor=0, 
        @active_start_date=20150119, 
        @active_end_date=99991231, 
        @active_start_time=0, 
        @active_end_time=235959, 
        @schedule_uid=N'a70709af-bce7-4c65-a4cd-7574acd31ca2'

The part that I want to replace is the following: 我要替换的部分如下:

, \r\n\t\t@schedule_uid=N'a70709af-bce7-4c65-a4cd-7574acd31ca2'

So that the final string is: 这样最后一个字符串是:

EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name='Job Name', 
        @enabled=1, 
        @freq_type=4, 
        @freq_interval=1, 
        @freq_subday_type=4, 
        @freq_subday_interval=10, 
        @freq_relative_interval=1, 
        @freq_recurrence_factor=0, 
        @active_start_date=20150119, 
        @active_end_date=99991231, 
        @active_start_time=0, 
        @active_end_time=235959

I've tried various combinations of things I've been reading online but I can't seem to make it replace or even match. 我已经尝试过在线阅读的各种组合,但是似乎无法替代甚至匹配。 I know that the regex for the guid matching is: 我知道guid匹配的正则表达式为:

\b[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}\b'

I've tried to add this into a number of things, and thought that the following regex would work but can't figure out what I'm doing wrong or missing 我试图将其添加到许多东西中,并认为以下正则表达式可以工作,但无法弄清楚我做错了什么或缺少什么

@", \r\n\t\t@schedule_uid=N'\b[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}\b'"
@", \r\n\t\t@schedule_uid=N'[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}'"
@", \r\n\t\t\b@schedule_uid=N'[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}'\b"

I'm not looking for a solution as much as I'd like to know what I'm missing. 我不是在寻找解决方案,而是想知道我缺少什么。 I've been reading the regular-expressions.info site for a while and I'm usually able to figure out the correct regex, but this has had me stumped for a few days now. 我已经阅读了一段时间的regular-expressions.info网站,通常能够找出正确的正则表达式,但这已经让我难过了几天。

EDIT: 编辑:

It's not always the last item and it's not guaranteed to only occur once within the script since a job can have multiple schedules which have different @schedule_uid's and I want to get rid of all of them without looping. 它并不总是最后一项,并且不能保证它只在脚本中出现一次,因为一个作业可以有多个具有不同@schedule_uid的日程表,而我想摆脱它们而不循环。 This is why I chose Regex for the operation. 这就是为什么我选择Regex进行操作的原因。 It also needs to remove the comma at the end of the previous parameters line for the code to remain syntax correct. 它还需要删除上一个参数行末尾的逗号,以使代码保持语法正确。

The following seems to work for me and it will enable you to remove all the newlines, tabs etc: 以下内容似乎对我有用,它将使您能够删除所有换行符,选项卡等:

(?:\n|\t|\r|.){1,3}.*\@sc.*'

You can see it working here 你可以看到它在这里工作

There you go: 你去了:

@schedule_uid=N'[\\w]{8}-[\\w]{4}-[\\w]{4}-[\\w]{4}-[\\w]{12}' @ schedule_uid = N '[\\ W] {8} - [\\ W] {4} - [\\ W] {4} - [\\ W] {4} - [\\ W] {12}'

Created and tested using http://regexpal.com/ 使用http://regexpal.com/创建并测试

A little more complicated, but works. 稍微复杂一点,但是可行。

string test = "EXEC...";

var lines = test.Split(new char [] { ',' }).ToList();

lines = lines.Select((line, index) => 
{
  var indexof = line.IndexOf("@schedule_uid");
  if (indexof > -1)
  {
    if (index == 0)
    {
      return line.Substring(0, indexof);
    }
    else
    {
      return null;
    }
  }
  return line + ",";
})
.Where(line => line != null)
.ToList();

test = string.Join(string.Empty, lines);

JsFiddle Example . JsFiddle示例

Assuming as little as possible, just using basic string operations. 假设使用基本的字符串操作,则尽可能地少。

        string exec = ...

        int i = exec.IndexOf("@schedule_uid");

        while (i > -1)
        {
            int j = i;
            //Find the previous comma
            while (exec[i] != ',')
                i--;
            //Find the end, next line, or next comma
            while (j < exec.Length && exec[j] != '\r' && exec[j] != ',')
                j++;
            exec = exec.Remove(i, j - i);
            i = exec.IndexOf("@schedule_uid");
        }

I'm deliberately ignoring the no looping requirement, in favour of simple code that works. 我故意忽略无循环要求,而是希望使用简单的代码。 tested vs this... 经过测试与这个...

        string exec = @"
        EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule, @schedule_uid=N'a70709af-bce7-4c65-a4cd-7574acd31ca2', @job_id=@jobId, @name='Job Name', 
    @enabled=1, 
    @freq_type=4, 
    @freq_interval=1, 
    @freq_subday_type=4, 
    @freq_subday_interval=10, 
    @freq_relative_interval=1, 
    @freq_recurrence_factor=0, 
    @schedule_uid=N'a70709af-bce7-4c65-a4cd-7574acd31ca2', 
    @active_start_date=20150119, 
    @active_end_date=99991231, 
    @active_start_time=0, 
    @active_end_time=235959, 
    @schedule_uid=N'a70709af-bce7-4c65-a4cd-7574acd31ca2'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM