Accept and return @something but reject first@last.
JavaScript
x
2
1
r'@([A-Z][A-Z0-9_]*[A-Z0-9])
2
The above regexp will accept @something (starts with letter, ends with letter or number, may have underscore in middle, atleast 2 characters long) and returns the part after the @
symbol.
I do not want to return strings which contain some letters or number A-Z0-9
before the @
symbol.
Spaces, new lines, special characters, etc before @
is allowed.
CODE:
JavaScript
1
2
1
re.findall(r'@([A-Z][A-Z0-9_]*[A-Z0-9])', text, re.I)
2
Advertisement
Answer
Use
JavaScript
1
2
1
re.findall(r'(?<![A-Z0-9])@([A-Z][A-Z0-9_]*[A-Z0-9])', text, re.I)
2
See regex proof.
EXPLANATION
JavaScript
1
21
21
1
--------------------------------------------------------------------------------
2
(?<! look behind to see if there is not:
3
--------------------------------------------------------------------------------
4
[A-Z0-9] any character of: 'A' to 'Z', '0' to '9'
5
--------------------------------------------------------------------------------
6
) end of look-behind
7
--------------------------------------------------------------------------------
8
@ '@'
9
--------------------------------------------------------------------------------
10
( group and capture to 1:
11
--------------------------------------------------------------------------------
12
[A-Z] any character of: 'A' to 'Z'
13
--------------------------------------------------------------------------------
14
[A-Z0-9_]* any character of: 'A' to 'Z', '0' to
15
'9', '_' (0 or more times (matching the
16
most amount possible))
17
--------------------------------------------------------------------------------
18
[A-Z0-9] any character of: 'A' to 'Z', '0' to '9'
19
--------------------------------------------------------------------------------
20
) end of 1
21