tags:

views:

63

answers:

1

Is it possible to have a named capture group that will always have the value of "BLAH" despite "BLAH" not appearing in the string being matched?

Effectively, I'm looking for something like the following pseudo-regex does (note the fake source=BLAH syntax):

^(?<source=BLAH>)$
+2  A: 

If I understood you correctly, you're asking whether you can have "source" act as a named capture group that will always have the value of "BLAH" despite "BLAH" not appearing in the string being matched. This isn't possible.

The most you can do is setup your regex with (?'source') and it will be considered an empty capture group that matches nothing. Using the GetGroupNames method, you could see that it exists, but you wouldn't be able to assign anything to it, which renders it useless. If "BLAH" is a file prefix that's expected then handle it elsewhere in your code, unless you have a variety of prefixes, in which case you may consider dynamically building the regex pattern to handle them.

Ahmad Mageed