Saurabh Kumar Saurabh Kumar - 3 months ago 7
R Question

search substring in a string with few fixed and rest any char

I have readLines from txt file.

Some line instances in text file are:

EAR99
EA
EAR99. NLR
EAFH9
Order Manag


There are more than 800 lines.

How could I find out line number which are starting with "EA***" (EA with three more alphanumeric character) substring.
Thus in my example I should get line number:- 1,3,4.
Line 2 is not qualified as it is on "EA".

Answer

We can use grep

grep("^EA[[:alnum:]]{3}", lines)
#[1] 1 3 4

data

lines <- readLines(textConnection("EAR99
EA
EAR99. NLR
EAFH9
Order Manag"))