0

I want to write a piece of code using regex that let's my replace ANY value inside my parenthesis. Take the following cases

Target text: build (123)

build (##-build-##)
build (111)
build (xxyyzz)

In all cases I want to find the word "build (*)" on the page, and replace it with my desired value.

3
  • Is there the possibility of nested parens? Commented Jan 10, 2013 at 1:11
  • Nope. This is just a hidden snippet of text at the bottom of our site for testers to look at. Commented Jan 10, 2013 at 1:11
  • I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". Commented Jan 10, 2013 at 1:44

3 Answers 3

2

Replace:

\b(build \()[^)]+(\))

with:

\1yourreplacementhere\2
Sign up to request clarification or add additional context in comments.

3 Comments

Uh? Won't this match only if the build part is the entire string?
Well, yes, and given the samples the OP has given, it looks like it will be the case. At least nothing tells otherwise (edit: no, I am mistaken, will edit).
"In all cases I want to find the word "build (*)" on the page" - I took this to mean that it was supposed to search the page... could be wrong though
1

Use pattern (?<=build\s)\([^)]*\):

var input = "build (##-build-##)";

var result = Regex.Replace(input, @"(?<=build\s)\([^)]*\)", "new value");

Console.WriteLine(result);

2 Comments

That will also remove the parens, if I have read correctly the OP wants to replace the value inside them, not remove them altogether
@fge Easy enough to throw parens into the new value, but should probably be mentioned in the answer.
1

Try this code, but be aware that it will disregard nested parenthesis:

var pattern = @"build \((.+)\)";
var regex = new Regex(pattern);
string[] strings =
{
    "build (##-build-##)",
    "build (111)",
    "build (xxyyzz)"
};
var results = strings.
    Select(s => regex.Replace(s, "(foo)")).
    ToArray();
//results = {build (foo), build(foo), build(foo)}

7 Comments

"be aware that it will disregard nested parenthesis" <-- OP says there can't be any, so this is safe. THere is another concern though, the word can appear several time in a page: your first .+ will swallow all until the end and give back to the last paren...
@fge I'm aware of the context the author wants to use it in, but I've adapted the example to map the input he provided to the desire output. It's unclear for me if he wants the Regex to operate on the entire page or on the strings taken singularly.
Well, OP says 'I want to find the word "build (*)" on the page', so... (and the fix is easy: use a complemented character class)
@fge I still think that the question was ambiguous. I'll edit my post making the pattern more specific. I didn't understand your first concern, about .+; he capitalized "ANY", which made me think that even whitespaces and new lines count.
I was a bit unclear, but I want to read in the entire contents of a file, find this string, and then replace it, and rewrite back out the file.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.