Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8470779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:36:56+00:00 2026-06-10T16:36:56+00:00

Say I have a regular expression like the following, but I loaded it from

  • 0

Say I have a regular expression like the following, but I loaded it from a file into a variable $regex, and so have no idea at design time what its contents are, but at runtime I can discover that it includes the “version1”, “version2”, “version3” and “version4” named groups:

"Version (?<version1>\d),(?<version2>\d),(?<version3>\d),(?<version4>\d)"

…and I have these variables:

$version1 = "3"
$version2 = "2"
$version3 = "1"
$version4 = "0"

…and I come across the following string in a file:

Version 7,7,0,0

…which is stored in a variable $input, so that ($input -match $regex) evaluates to $true.

How can I replace the named groups from $regex in the string $input with the values of $version1, $version2, $version3, $version4 if I do not know the order in which they appear in $regex (I only know that $regex includes these named groups)?

I can’t find any references describing the syntax for replacing a named group with the value of a variable by using the group name as an index to the match – is this even supported?

EDIT:
To clarify – the goal is to replace templated version strings in any kind of text file where the version string in a given file requires replacement of a variable number of version fields (could be 2, 3, or all 4 fields). For example, the text in a file could look like any of these (but is not restricted to these):

#define SOME_MACRO(4, 1, 0, 0)

Version "1.2.3.4"

SomeStruct vs = { 99,99,99,99 }

Users can specify a file set and a regular expression to match the line containing the fields, with the original idea being that the individual fields would be captured by named groups. The utility has the individual version field values that should be substituted in the file, but has to preserve the original format of the line that will contain the substitutions, and substitute only the requested fields.

EDIT-2:
I think I can get the result I need with substring calculations based on the position and extent of each of the matches, but was hoping Powershell’s replace operation was going to save me some work.

EDIT-3:
So, as Ansgar correctly and succinctly describes below, there isn’t a way (using only the original input string, a regular expression about which you only know the named groups, and the resulting matches) to use the “-replace” operation (or other regex operations) to perform substitutions of the captures of the named groups, while leaving the rest of the original string intact. For this problem, if anybody’s curious, I ended up using the solution below. YMMV, other solutions possible. Many thanks to Ansgar for his feedback and options provided.

In the following code block:

  • $input is a line of text on which substitution is to be performed
  • $regex is a regular expression (of type [string]) read from a file that has been verified to contain at least one of the supported named groups
  • $regexToGroupName is a hash table that maps a regex string to an array of group names ordered according to the order of the array returned by [regex]::GetGroupNames(), which matches the left-to-right order in which they appear in the expression
  • $groupNameToVersionNumber is a hash table that maps a group name to a version number.

Constraints on the named groups within $regex are only (I think) that the expression within the named groups cannot be nested, and should match at most once within the input string.

# This will give us the index and extent of each substring
# that we will be replacing (the parts that we will not keep)
$matchResults = ([regex]$regex).match($input)

# This will hold substrings from $input that were not captured
# by any of the supported named groups, as well as the replacement
# version strings, properly ordered, but will omit substrings captured
# by the named groups
$lineParts = @()
$startingIndex = 0
foreach ($groupName in $regexToGroupName.$regex)
{
    # Excise the substring leading up to the match for this group...
    $lineParts = $lineParts + $input.Substring($startingIndex, $matchResults.groups[$groupName].Index - $startingIndex)

    # Instead of the matched substring, we'll use the substitution
    $lineParts = $lineParts + $groupNameToVersionNumber.$groupName

    # Set the starting index of the next substring that we will keep...
    $startingIndex = $matchResults.groups[$groupName].Index + $matchResults.groups[$groupName].Length
}

# Keep the end of the original string (if there's anything left)
$lineParts = $lineParts + $input.Substring($startingIndex, $input.Length - $startingIndex)

$newLine = ""
foreach ($part in $lineParts)
{
   $newLine = $newLine + $part
}
$input= $newLine
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:36:58+00:00Added an answer on June 10, 2026 at 4:36 pm

    Regular expressions don’t work that way, so you can’t. Not directly, that is. What you can do (short of using a more appropriate regular expression that groups the parts you want to keep) is to extract the version string and then in a second step replace that substring with the new version string:

    $oldver = $input -replace $regexp, '$1,$2,$3,$4'
    $newver = $input -replace $oldver, "$Version1,$Version2,$Version3,$Version4"
    

    Edit:

    If you don’t even know the structure, you must extract that from the regular expression as well.

    $version = @($version1, $version2, $version3, $version4)
    $input -match $regexp
    $oldver = $regexp
    $newver = $regexp
    for ($i = 1; $i -le 4; $i++) {
      $oldver = $oldver -replace "\(\?<version$i>\\d\)", $matches["version$i"]
      $newver = $newver -replace "\(\?<version$i>\\d\)", $version[$i-1]
    }
    $input -replace $oldver, $newver
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say for example that I have a regular expression like this: The quick
Say I have a regular expression like the fowllowing: {(group 1) | (group 2)
I have a regular expression, say /url.com\/([A-Za-z]+)\.html/ , and I would like to replace
Say i have a few fields like the following: abd738927 jaksm234234 hfk342 ndma0834 jon99322
Let's say I have a string like so: $file = 'widget-widget-newsletter.php'; I want to
This may be a silly question but... Say you have a sentence like: The
Using .NET regular expressions. Let's say I have the following text: ddddddddddd And I
Suppose I have a lex regular expression like [aA][0-9]{2,2}[pP][sS][nN]? { return TOKEN; } If
Let's say I have a bunch of text like this (simplified example, but you
Say I have generated the following binary file: # generate file: python -c 'import

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.