-3

Suppose I have a string with the following pattern: a bunch of text I don't care about, followed by some text that is enclosed in "[" and "]". Within the enclosed space, I have some text enclosed in "(" and ")". This second set of text I want to get rid of. At the end, I'd like to have returned the stuff within the first enclosing, which I'll then replace with new text:

Blah [(remove)Field:(remove)Name(remove)] Blah

What I'd like to get returned from that above example would be the following (in two steps to make it more clear):

First:

Blah [Field:Name] Blah

Then:

Blah ReplacedText Blah

How would I do this with a Regex.Replace?

** EDIT **

In fact, ideally, if I could actually get back the value "Name" in the above example, for my replacing, it would be even better. So in other words, replace [Field:Name] with a value, but obtain "Name" in a group as well.

Kiran Ramaswamy
  • 605
  • 1
  • 8
  • 19
  • 1
    I'd start on one of the online regex testers to try out patterns that match these groups. Then I'd ask a question on SO if there was a specific issue I was running into. – C.Evenhuis Oct 16 '17 at 14:51
  • 1
    Sounds like a plan, what have you tried? – jdmdevdotnet Oct 16 '17 at 14:53
  • 2
    I'm voting to close this question as off-topic because straight up asking for regex patterns with no pre-existing attempt is [off topic](https://meta.stackoverflow.com/questions/285733/should-give-me-a-regex-that-does-x-questions-be-closed). – gunr2171 Oct 16 '17 at 14:55
  • Possible duplicate of [Regular expression to extract text between square brackets](https://stackoverflow.com/questions/2403122/regular-expression-to-extract-text-between-square-brackets) – TylerH Oct 16 '17 at 15:01
  • Try you code with `foo [foo bar] bar Field: foo Name bar [foobar]` and `Blah [(][)Field:(]()Name(][)] Blah` And learn about greedy https://www.regular-expressions.info/repeat.html#greedy – Drag and Drop Oct 16 '17 at 15:20

3 Answers3

1
string text = "Blah [(remove)Field:(remove)Name(remove)] Blah";
string replacement = "ReplacedText";
string pattern = @"\[ \( .*? \) Field: \( .*? \) (?'name' .*? ) \( .*? \) \]";

string name = null;

var result = Regex.Replace(text, pattern, m =>
{
    name = m.Groups["name"].Value;
    return replacement;
},
RegexOptions.IgnorePatternWhitespace);


Console.WriteLine(name);
Console.WriteLine(result);
Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
  • Hey - this puts me on the right track. Thanks! – Kiran Ramaswamy Oct 16 '17 at 15:00
  • 1
    While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – gunr2171 Oct 16 '17 at 15:01
  • @gunr, how should I go about that? Should I post it as an answer, or should I put it as a comment in response to this answer? – Kiran Ramaswamy Oct 16 '17 at 15:03
  • I very bad speak English, so feel free to edit post. – Alexander Petrov Oct 16 '17 at 15:05
  • @KiranRamaswamy that comment was not meant for you, but Alexander. – gunr2171 Oct 16 '17 at 15:05
  • 1
    `[^]]` Should be concider. – Drag and Drop Oct 16 '17 at 15:14
0

First step, use :

(\(.*?\))

And replace the matches by an empty string (just to remove them). So the result will be :

Blah [Field:Name] Blah

Second step, use almost the same regex :

(\[.*?\])

And replace the matches with whatever text you want. Result will then be :

Blah ReplacedText Blah

Edit :

If you want to keep the information on the Name as you added in your edit, you could use this regex in place of the second one :

(\[.*?:(.*?)\])

This way, the first group will hold the text that you want to replace, and the second group will hold the value on what follows the ':', in your case 'Name'.

Paul-Etienne
  • 796
  • 9
  • 23
0

Here's a simple example using RegEx:

var pattern = @"\[.*Field:.*Name.*\]";
var r = new System.Text.RegularExpressions.Regex(pattern);
var newText = r.Replace("Blah [1ad3d Field: (stuff) Name    ]  Blah", "My Custom Replacement Text");
Console.WriteLine(newText);  // newText="Blah My Custom Replacement Text  Blah"
SlimsGhost
  • 2,849
  • 1
  • 10
  • 16