tags:

views:

55

answers:

3

Hi guys, I'm not very familiar to regex and even not able (maybe too tired?) to use this silly newby issue:

I need a regex, that allows any combination of numbers, letters (lower and upper case) and the underscore _

BUT: The beginning of this regex shall be fix and defined in my source code::

ABC_h2u3h4l
ABCijij4i5oi4j5
ABCABC

Here the piece "ABC" always has to be at the leading position.

Can someone give me a hint?

+1  A: 
^ABC[a-zA-Z0-9_]*$
adamk
I think that the regex should have a `^` at the beginning. Otherwise it won't guarantee that the string starts with `ABC`. It would match a string such as `123ABC`.
sigint
Changed, thanks for the comment.
adamk
+3  A: 

that's the whole regex:

^ABC\w+
SilentGhost
why the downvote?
SilentGhost
The ABC must match at the beginning of the line. This will match anywhere.
cape1232
Not necessarily (even without the `^`) - for example, in Python `re.match()` always anchors the match at the start of the string. But I'd add a `$` at the end, or the regex will also return True on a partially matched string.
Tim Pietzcker
@Tim: it's not clear from the question whether there is a single multi-line string given and every line should match (which with my regex would require `re.M` flag) or it's a series of string which could be matched with `re.match` (with or without the `^` anchor).
SilentGhost
+3  A: 

Something like this?

^ABC\w+$
LukeH