0

I am trying to match a non-capture group inside a named capture group in a .NET regular expression, but for some reason the non-capture group is still being captured:

  • input: "A/B"
  • actual output: Token = "A/B"
  • desired output: Token = "AB"

my attempt using a non-capture group:

(?<Token>\w(?:/)\w)

I've got a feeling that I need to use a lookahead and/or a lookbehind here, but I still can't get it working. Any ideas?

Adriaan
  • 51
  • 3
  • `(?:/)` is equivalent to just `/`. – Bergi May 09 '14 at 05:13
  • Can you use a replace instead of a match and store the result of the replace in a variable? – Jerry May 09 '14 at 05:17
  • No unfortunately not. Further searching on SO led me to [this](http://stackoverflow.com/questions/277547/regular-expression-to-skip-character-in-capture-group) question, so I guess it's just not possible – Adriaan May 09 '14 at 05:22
  • Can you do a regexp replace on the string before extracting the token? – Taemyr May 09 '14 at 07:52

2 Answers2

2

The (?:/) part is still contained within the capture group, it just doesn't form its own capture group. You could do:

(\w)(?:/)(\w)

This will capture the A and B in separate capture groups, but that doesn't do exactly what you want.

What you can do is capture the entire group (?<Token>\w/\w) and then do a string replace of / with nothing.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • 1
    Thanks for your input. I unfortunately can't perform any post processing on the regex's match result. I need to extract the final token using a single expression – Adriaan May 09 '14 at 05:16
  • 1
    @Adriaan I'm afraid you're stuck since regexen simply don't work that way. Can you do a regex replace? – Explosion Pills May 09 '14 at 05:19
0

Try This:

1.9.3-p545 :022 > line = "A/B"
 => "A/B" 
1.9.3-p545 :023 > test = line.scan(/([A-z: ]+)/).flatten.join()
 => "AB" 
1.9.3-p545 :024 > 
anusha
  • 2,087
  • 19
  • 31