admin管理员组

文章数量:1265381

I need a Regular Expression that matches everything after the first two characters in a string.

For Example (original string listed first, then what I'd like to match):

AZ0bc1234  > 0bc1234
50def123   > def123
!@hijk1234 > hijk1234

All that matters is position, any characters (alpha-numeric with symbols) could be included in the original string.

I've tried many things, but everything I attempt matches at least one of the first two characters. The closest I've e is using \B.* to match everything but the first character.

I need a Regular Expression that matches everything after the first two characters in a string.

For Example (original string listed first, then what I'd like to match):

AZ0bc1234  > 0bc1234
50def123   > def123
!@hijk1234 > hijk1234

All that matters is position, any characters (alpha-numeric with symbols) could be included in the original string.

I've tried many things, but everything I attempt matches at least one of the first two characters. The closest I've e is using \B.* to match everything but the first character.

Share Improve this question edited Jan 14, 2013 at 16:46 Anirudha 32.8k8 gold badges71 silver badges90 bronze badges asked Jan 14, 2013 at 16:37 Matt KMatt K 7,3376 gold badges41 silver badges62 bronze badges 5
  • I'll have a proper look at it, but in the meantime you might find this link useful www.regexlib. – CHill60 Commented Jan 14, 2013 at 16:39
  • What language are you using? Are you sure you need to use a regex? If you're using PHP, for example, you could use the substr() function. – Andy Lester Commented Jan 14, 2013 at 16:39
  • I understand there are easier ways of doing this, but I'm curious if it's theoretically possible using regular expressions exclusively. – Matt K Commented Jan 14, 2013 at 16:41
  • you need to specify the language you are using – Anirudha Commented Jan 14, 2013 at 16:44
  • I'm attempting this in JavaScript. – Matt K Commented Jan 14, 2013 at 16:45
Add a ment  | 

3 Answers 3

Reset to default 7

You were looking for a positive lookbehind. this will only match the part you've requested.

(?<=.{2})(.*)$

You've updated your question and wrote that you use JavaScript. Lookbehind is not supported in JavaScript. However I will leave this answer for future search results.

If you want everything but the first two characters, you could try this (to capture up to the end of each line):

".{2}(.*)$"

You are after the first group (in parens). Or differently:

"(?:.{2})(.*)$"

The following will hold the matching string in the capturing group (define by the parenthesis) :

^.{2}(.+)

You should be able to use it with $1 or \1 depending on the language.

本文标签: javascriptRegular Expression to match everything but the first two charactersStack Overflow