How to replace a matched group value with Regex

Mitulát báti

I would like to modify the value of the "data source" component in a connection string. I'm thinking of the following solution:

Using this regex pattern:

"data source\=((\w|\-)+?\\{1}\w+?)\;"

I can obtain the following string matches:

Match.Groups[0].Value = "data source=MY-PC\SQLEXPRESS;"
Match.Groups[1].Value = "MY-PC\SQLEXPRES"

So in the connection string firstly I would like to find the part matching with the "data source=something;", and secondly replace just the "something" in the connection string. How to do that?

Diryboy

If you insist Regex replacing, please note C# cannot modify a built string, you need to get a new string with the needed part replaced.

var connectionString = @"data source=MY-PC\SQLEXPRESS;";
var pattern = @"(data source=)((\w|\-)+?\\\w+?)\;";
var newConnectionString = Regex.Replace(connectionString, pattern, "$1" + "something");
Console.WriteLine(newConnectionString);

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

python how to replace string by regex group?

분류에서Dev

Placing each matched value in its own capturing group

분류에서Dev

How to get index of regex match of only the matched and included part?

분류에서Dev

R regex match pattern and replace with dynamic value

분류에서Dev

How to replace input text tag into plain text of it's value using C# regex?

분류에서Dev

How to replace character in capture group with sed

분류에서Dev

How to replace an object value in an array?

분류에서Dev

How to show full results, rather than matched text from regex searches in python

분류에서Dev

Notepad regex replacement : how to split and replace a result?

분류에서Dev

Regex - how does Matcher.Group work

분류에서Dev

Create regex to replace the href in input value with single quotes

분류에서Dev

Is there a more robust way to edit the pattern matched, and then replace it?

분류에서Dev

How to replace the column value in select statement in Sql?

분류에서Dev

How to match parameter and replace the value after "=" separator

분류에서Dev

How to replace value with square brackets using sed?

분류에서Dev

How to replace the user IDs and group IDs with names instead of numbers in "ps"?

분류에서Dev

How to combine field value when use group by

분류에서Dev

Perl regex: how to replace terminal control characters with html tags?

분류에서Dev

Regex - indexof - split and replace how should I apply them correctly?

분류에서Dev

How to run regex find and replace using windows command line

분류에서Dev

How to find a value in one column and replace a value in different column in excel?

분류에서Dev

How do I change this value using regex?

분류에서Dev

JAVA REGEX : How do I find the exact matching group?

분류에서Dev

pyparsing to group matched string and unmatched stings in the same order the input text

분류에서Dev

Python RegEx Remove / Replace

분류에서Dev

Regex replace priority

분류에서Dev

PHP Regex replace tags

분류에서Dev

Clojure replace a string with regex

분류에서Dev

Regex Replace Not Matching

Related 관련 기사

  1. 1

    python how to replace string by regex group?

  2. 2

    Placing each matched value in its own capturing group

  3. 3

    How to get index of regex match of only the matched and included part?

  4. 4

    R regex match pattern and replace with dynamic value

  5. 5

    How to replace input text tag into plain text of it's value using C# regex?

  6. 6

    How to replace character in capture group with sed

  7. 7

    How to replace an object value in an array?

  8. 8

    How to show full results, rather than matched text from regex searches in python

  9. 9

    Notepad regex replacement : how to split and replace a result?

  10. 10

    Regex - how does Matcher.Group work

  11. 11

    Create regex to replace the href in input value with single quotes

  12. 12

    Is there a more robust way to edit the pattern matched, and then replace it?

  13. 13

    How to replace the column value in select statement in Sql?

  14. 14

    How to match parameter and replace the value after "=" separator

  15. 15

    How to replace value with square brackets using sed?

  16. 16

    How to replace the user IDs and group IDs with names instead of numbers in "ps"?

  17. 17

    How to combine field value when use group by

  18. 18

    Perl regex: how to replace terminal control characters with html tags?

  19. 19

    Regex - indexof - split and replace how should I apply them correctly?

  20. 20

    How to run regex find and replace using windows command line

  21. 21

    How to find a value in one column and replace a value in different column in excel?

  22. 22

    How do I change this value using regex?

  23. 23

    JAVA REGEX : How do I find the exact matching group?

  24. 24

    pyparsing to group matched string and unmatched stings in the same order the input text

  25. 25

    Python RegEx Remove / Replace

  26. 26

    Regex replace priority

  27. 27

    PHP Regex replace tags

  28. 28

    Clojure replace a string with regex

  29. 29

    Regex Replace Not Matching

뜨겁다태그

보관