25

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?

3 Answers 3

40

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 you can do it like this: ^.{3}[0-9]{5}$
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)/ 

1 Comment

@tchelidze Adding $ will solve the problem (if OP wants). I'm not sure what language/tool they're using.
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.