24

I need to come up with a regex to look for only letters A, F or E on the position 9 of a given text. I am really new with regex, did some searching and couldn't find any similar response. what i have so far is:

/^.{9}A/

This command seems to work to find letter A on the space nine, but how can I add the other 2 letters to the regex?

Daniel Braga
  • 243
  • 1
  • 2
  • 5

3 Answers3

38

You say you're looking for C, F or E but looking for A in your example, so please include in the brackets any other letters you want to match, but what you're looking for is:

/^.{8}[CFE]/

It should be {8} rather than {9} because the way you had it, it'll match the first 9 characters and then match your letter in position 10.

John Clifford
  • 821
  • 5
  • 10
  • I am trying to do a regex match so that a password is max 8 characters long and the 4th character has to be a digit ^{3}[0-9].[a-z0-9]{8}$ this is what I have so far but I believe it is wrong – user6248190 Dec 18 '17 at 11:29
  • @user6248190 you can do it like this: `^.{3}[0-9]{5}$` – E235 Sep 26 '18 at 11:03
4

Use a character class:

/^.{9}[CFE]/
  • [CFE] matches one of C, F or E

Or use the | meta-character (alternation):

/^.{9}(?:C|F|E)/ 
Maroun
  • 94,125
  • 30
  • 188
  • 241
  • @tchelidze Adding `$` will solve the problem (if OP wants). I'm not sure what language/tool they're using. – Maroun Jan 14 '16 at 12:48
1

This way can also solve the problem

/^.{8}A|^.{8}F|^.{8}E/;

you have to use 8 instead of 9 if you want to match the character on the 9th position

hacker
  • 27
  • 1
  • 9