3

I would like to make a pattern.

For example, my input is

string str = "15-16-00-014716 AND15- [  ] (5) Description of 16-00-014715";

Expected output is

15-16-00-014716 AND15-16-00-014715

I tried below regex:

Regex.Replace(YourString, @"\s+\[.*(?=\b\d+)","");

But the output is like

15-16-00-014716 AND15-0-014715
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
  • 1
    Try `(?s)\s+\[.*\n(?=\d)` pattern. What is the rule here, BTW? Why is your pattern like this? Maybe `\s+\[.*\n.*\n` or `\s+\[(?:.*\n){2}` is enough – Wiktor Stribiżew May 07 '19 at 10:16
  • Someone asked a similar question yesterday. [Check this](https://stackoverflow.com/questions/56000369/remove-some-specific-string-with-special-character/56000405#56000405) Also for your current post, you can use this regex [`(?s)\s+\[.*(?=\b\d+-\d+-)`](https://regex101.com/r/dcArSI/1) or [`(?s)\s+\[.*?(?=\b\d+-)`](https://regex101.com/r/dcArSI/2) – Pushpesh Kumar Rajwanshi May 07 '19 at 10:23
  • If you explain the rules, I think a better solution may be suggested. Why regex? – Wiktor Stribiżew May 07 '19 at 10:26
  • @PushpeshKumarRajwanshi Actually, I want a single regex for below both strings As you provide a solution but it works on 2 number string, not 1 number. 1. "14-03-002980 AND 14-03- [ ] (5)Description of 002981" 2. "15-16-00-014716 AND15- [ ] (5)Description of 16-00-014715" – Dhaval Patel May 07 '19 at 10:31
  • @DhavalPatel Ok, does `\s+\[(?:.*\n){2}` work for you? See [.NET regex demo](http://regexstorm.net/tester?p=%5cs%2b%5c%5b%28%3f%3a.*%5cn%29%7b2%7d&i=15-16-00-014716+AND15-+%5b++%5d+%285%29%0d%0aDescription+of+%0d%0a16-00-014715%0d%0a%0d%0a15-16-00-014716+AND15-+%5b++%5d+%285%29%0d%0aDescription+of+%0d%0a1600014715&r=). It removes a substring beginning with whitespaces, `[`, the rest of the line and the next line. – Wiktor Stribiżew May 07 '19 at 10:40
  • Sorry, Not Worked – Dhaval Patel May 07 '19 at 10:48
  • @DhavalPatel Show your full relevant code. Add it to the question. As it is now, it is not reproducible as the input text is not a valid string literal, and it is not clear if there are any linebreaks in fact, or you just formatted it so for SO – Wiktor Stribiżew May 07 '19 at 10:52
  • @WiktorStribiżew void Main() { string str="15-16-00-014716 AND15- [ ] (5) Description of 16-00-014715"; string a= Regex.Replace(str, @"\s+\[(?:.*\n){2}",""); a.Dump(); } – Dhaval Patel May 07 '19 at 10:55
  • @DhavalPatel: Can you add your samples in your post with expected vs current output? It is confusing whether you have newlines in your input string or not. – Pushpesh Kumar Rajwanshi May 07 '19 at 10:55
  • I think you need `Regex.Replace(str, @"\s+\[.*?(?=\b\d+(?:-|$))", "");`. See https://ideone.com/S7Emqu. So, it means you do not have any line breaks in the input. – Wiktor Stribiżew May 07 '19 at 10:57
  • @PushpeshKumarRajwanshi void Main() { String str="14-03-002980 AND 14-03- [ ] (5)Description of 002981"; string str1="15-16-00-014716 AND15- [ ] (5) Description of 16-00-014715"; string a= Regex.Replace(str, @"\s+\[(?:.*\n){2}",""); a.Dump(); } Expected out For Both String like below "14-03-002980 AND 14-03-002981" "15-16-00-014716 AND15-16-00-014715" – Dhaval Patel May 07 '19 at 10:59
  • See https://ideone.com/S7Emqu, I updated the demo – Wiktor Stribiżew May 07 '19 at 11:00
  • @DhavalPatel I posted [an answer](https://stackoverflow.com/a/56021148/3832970). – Wiktor Stribiżew May 07 '19 at 11:07

1 Answers1

3

You may use

Regex.Replace(str, @"\s+\[.*?(?=\b\d+(?:-|$))", "")

See the C# demo and the regex demo.

Regex graph:

enter image description here

Main points:

  • .* should be non-greedy (.*? matches as few any chars as possible)
  • (?:-|$) matches a - char or end of string position.
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397