What does this regular expression mean \w+(?=@). Im trying to understand how this expression will pull users from the AD, currently it is omitting all characters before a special Character. Ex: Viny.Trucker will result only in .Truck. I want the whole name Viny.Trucker to be extracted. Any help is greatly appreciated.
5 Answers
It means find "any number of word characters (a-z, A-Z, 0-9 and _) immediately followed by an @ symbol. The @ symbol won't be included in the matched characters. If you want to include the '.' character in the expression, you can try (\w|.)+(?=@)

- 4,473
- 2
- 29
- 35
-
`\w` includes also `[0-9_]`. – Toto Apr 16 '14 at 11:56
Try this : http://www.regexr.com/ It comes with very nice explanation when you hover over chars. And here is your regex : http://www.regexr.com/38ndp

- 2,749
- 2
- 25
- 39
It matches the characters before an @-character.
\w+ Match at least one word-character, i.e. letters, numbers and underscores.
(?=@) Match an @-character without including it in the match.

- 10,940
- 8
- 45
- 80
Jan AAgaard is right, it matches all word characters before an @
.
How does it do this?
\w+ Matches ones or more word-chracters.
(?=@) This is called a positive-lookahead. It means require the following to come after the characters, but don't include that in the match.
A better alternative
Now, it seems that you want to match every character before an @
, the easy way to to this is
[^@]+(?=@)
If you know that there will always be a @
in a line you could even use
[^@]+
and just take the first match.
If there will always be a certain set of chracters allowed before the @
, you could even specify those in a character class: Simply put all of them in square brackets, e.g.:
[a-zA-Z0-9._]

- 1,403
- 1
- 11
- 19
It's searching for any character, digit or underscore
followed by @
\w+(?=@)
Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=@)»
Match the character “@” literally «@»
Positive and Negative Lookahead
Negative lookahead is indispensable if you want to match something not followed by something else. When explaining character classes, this tutorial explained why you cannot use a negated character class to match a q not followed by a u. Negative lookahead provides the solution: q(?!u). The negative lookahead construct is the pair of parentheses, with the opening parenthesis followed by a question mark and an exclamation point. Inside the lookahead, we have the trivial regex u.
Positive lookahead works just the same. q(?=u) matches a q that is followed by a u, without making the u part of the match. The positive lookahead construct is a pair of parentheses, with the opening parenthesis followed by a question mark and an equals sign. You can use any regular expression inside the lookahead (but not lookbehind, as explained below). Any valid regular expression can be used inside the lookahead. If it contains capturing groups then those groups will capture as normal and backreferences to them will work normally, even outside the lookahead. (The only exception is Tcl, which treats all groups inside lookahead as non-capturing.) The lookahead itself is not a capturing group. It is not included in the count towards numbering the backreferences. If you want to store the match of the regex inside a lookahead, you have to put capturing parentheses around the regex inside the lookahead, like this: (?=(regex)). The other way around will not work, because the lookahead will already have discarded the regex match by the time the capturing group is to store its match.

- 94,083
- 31
- 258
- 268