Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex to match character in specific position

Tags:

regex

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?

like image 889
Daniel Braga Avatar asked Jan 14 '16 12:01

Daniel Braga


People also ask

How do I match a character in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

What does .*?) Mean in regex?

(. *?) matches any character ( . ) any number of times ( * ), as few times as possible to make the regex match ( ? ). You'll get a match on any string, but you'll only capture a blank string because of the question mark.

What does $1 do in regex?

For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group.


1 Answers

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.

like image 194
John Clifford Avatar answered Oct 14 '22 04:10

John Clifford