Saurabh Kumar
Saurabh Kumar

Reputation: 13

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".

Upvotes: 1

Views: 41

Answers (1)

akrun
akrun

Reputation: 887571

We can use grep

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

data

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

Upvotes: 2

Related Questions