1

It is common way to replace a pattern match with string, but I need to replace all sub-strings which match pattern from text to be matched as another pattern match, is it possible?

For example, is it possible to replace all matches to [0-9]{2}'[0-9]{2} which represent all strings like 99'99 or 85'55

To this [0-9]{2}.[0-9]{2} which represent all strings like 99.99 or 85.55

Is it possible? How to do this kind of replacements? or I have to handle it manually through matches in for each loop?

6
  • 1
    Do you mean the capturing groups: ([0-9]{2})'([0-9]{2}) > $1.$2? Commented Mar 17, 2016 at 16:23
  • 4
    @WiktorStribiżew: If a Guru like you gives correct answer in comments how are noobs like me going to get chance to solve some simpler problems and rise up the ranks ? Commented Mar 17, 2016 at 16:30
  • @WiktorStribiżew: Good point :) Commented Mar 17, 2016 at 16:31
  • @WiktorStribiżew: The current comment is the edited one asking for clarification. The first one was rather a direct demo of solution. Correct me if am wrong. If you had posted it as solution there wasn't a problem at all. But posting a correct solution in comment gives other users who might not have solution, copy yours. And if a genuine users posts his solution which is similar ( it's regex so most are similar ) it will be down voted considering it as copying answers. Commented Mar 17, 2016 at 16:51
  • If your question is to replace sub-strings like 99'99 to 99.99, then it is as simple as to replace ' with .. Otherwise, please edit your post with another proper example. Commented Mar 17, 2016 at 18:04

2 Answers 2

1

Use the Regex.Replace() instance function along with Regex capture groups like this:

var regex = new Regex("([0-9]{2})'([0-9]{2})");
string result = regex.Replace(input, "$1.$2");

More details about capture groups can be found here.

Also, check out this answer. It shows how to use 'named' groups which might help in future.

Sign up to request clarification or add additional context in comments.

Comments

0

So as I understand you, you have something like 99'99 or 85'55 and want this to be replaced by 99.99 or 85.55?

What you might look for are capture groups, i.e. find for a matching, catch this matching and put this to the result.

the RegEx here would be s/([0-9]{2})'([0-9]{2})/$1.$2/g

Explanation: ([0-9]{2}) the brackets declare the caption group. It means, whatever is captured in it, will be stored to a variable. These Variables are $1 and $2, because there were two capture groups.

When building the replacement string, just insert those variables and put the dot between them.

5 Comments

It's dot not comma.
Why does your regex have replacement part in it ?
because the question asker asked how to do the replacement.
I don't mean to say why you are doing replacement. I mean replacement part won't be appearing in regex. It will be separate from regex. See the answer below.
I didn't see the C# tag at first and this question seemed to be a 'standard' RegEx question, so I provided a RegEx which solves the problem, checked it with perl (in this case, it does appears in the regex).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.