x Hint: Register, if you want to hide ads.
x Did you like it? Well, then please consider making a donation :)

Replace and named capturing group example

Nearly all modern regular expression engines support numbered capturing groups and numbered backreferences. Long regular expressions with lots of groups and backreferences may be hard to read. They can be particularly difficult to maintain as adding or removing a capturing group in the middle of the regex upsets the numbers of all the groups that follow the added or removed group.

Tags: #captures, #group, #hour, #minutes, #seconds, #time. Views: 1921. admin, 3/30/2015
Link 1: http://www.systemtextregularexpressions.com/r5/replace-and-named-capturing-group-example
Link 2: http://www.systemtextregularexpressions.com/r5 (short)


Length: 0b, Line: 1, Cursor: 0

RegexOptions

IsMatch=true

Result

Replace text example and captures the matched subexpression into a named group. Example 1: Current time: 02h 21m 32s Example 2: Current time: 16h 45m 56s Example 3: Current time: 19h 02m 11s

Share (without adding to database):

Source code

0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
//.NET Regex.Replace Online Tester: www.systemtextregularexpressions.com

//Namespace
using System.Text.RegularExpressions;

//Regex Pattern
string pattern = @"(?<hour>\d{2}):(?<minute>\d{2}):(?<second>\d{2})";

//Regex example #1
Regex r = new Regex(@"(?<hour>\d{2}):(?<minute>\d{2}):(?<second>\d{2})");
string result1 = r.Replace("input text", "replacement text");

//Regex example #2
string result2 = Regex.Replace("input text", @"(?<hour>\d{2}):(?<minute>\d{2}):(?<second>\d{2})", "replacement text");
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
'.NET Regex.Replace Online Tester: www.systemtextregularexpressions.com

'Namespace
Imports System.Text.RegularExpressions

'Regex Pattern
Dim pattern As String = "(?<hour>\d{2}):(?<minute>\d{2}):(?<second>\d{2})"

'Regex example #1
Dim r As Regex = New Regex("(?<hour>\d{2}):(?<minute>\d{2}):(?<second>\d{2})")
Dim result1 As String = r.Replace("input text", "replacement text")

'Regex example #2
Dim result2 As String = Regex.Replace("input text", "(?<hour>\d{2}):(?<minute>\d{2}):(?<second>\d{2})", "replacement text")