Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex match first characters of string

Tags:

regex

I am trying to create a regex that will match the first 3 characters of a string,

If I have a string ABCFFFF I want to verify that the first 3 characters are ABC.

like image 530
installedConfusion Avatar asked Jun 03 '11 15:06

installedConfusion


2 Answers

It's pretty straightforward, the pattern would be ^ABC

As others may point out, using regular expressions for such a task is an overkill. Any programming language with regex support can do it better with simple string manipulations.

like image 72
Dyppl Avatar answered Oct 18 '22 20:10

Dyppl


Just simple regex will work:

/^ABC/

But is it a good use case for using regex, I am not sure. Consider using substring in whatever language/platform you're using.

like image 33
anubhava Avatar answered Oct 18 '22 20:10

anubhava