In RegEx, can I "hardcode" assign a capture group value

Is it possible to have a named capture group that will always have the value "BLAH" even though "BLAH" does not appear on the corresponding line?

In fact, I'm looking for something like the following pseudo-regular expression (note the fake source = BLAH syntax):

^(?<source=BLAH>)$ 
+4
source share
1 answer

If you understand correctly, you ask if you can have a “source” as a named capture group that will always have the value “BLAH”, even though “BLAH” does not appear on the line that maps. It's impossible.

The most you can do is customize your regex with (? 'Source') and this will be considered an empty capture group that doesn't match anything. Using the GetGroupNames method, you can see that it exists, but you cannot assign anything to it, which makes it useless. If “BLAH” is the file prefix, which is then expected to be processed elsewhere in your code, if you do not have many prefixes, then you might consider dynamically creating a regular expression pattern to process them.

+3
source

All Articles