Abdul Salam
Abdul Salam

Reputation: 552

Regex to Capture a string

From the Test string I need to capture, the string Ec2 Instances which are ... .micro this string is repeated in my Test string with slight changes.

Like, stopped or running Instance ID could change

I tried

^Ec2 Instances.*micro$

But its not working for me.

Test String

 'ingestionTime': 1526375668364, 'eventId': '34039314519276244324908423608603573165487083445254619137'}, {'logStreamName': '2018/05/15/[$LATEST]8b2aa0fa731f4534afc62a106ab3aead', 'timestamp': 1526375653280, 'message':"Ec2 Instances which are running:  Instance ID:  i-006690f105487930f Instance state:  {'Code': 16, 'Name': 'running'} Instance type:  t2.micro", 'ingestionTime': 1526375668364, 'eventId': '34039314521038003195592342836784894909026304004227072002'}, {'logStreamName': '2018/05/15/[$LATEST]8b2aa0fa731f4534afc62a106ab3aead', 'timestamp': 1526375653280, 'message': 'END RequestId: 553e166e-5820-11e8-9bd1-0d6fafd1c3b2', 'ingestionTime': 1526375668364, 'eventId': '34039314521038003195592342836784894909026304004227072003'}, {'logStreamName': '2018/05/15/[$LATEST]8b2aa0fa731f4534afc62a106ab3aead', 'timestamp': 1526375653280, 'message': 'REPORT RequestId: 553e166e-5820-11e8-9bd1-0d6fafd1c3b2\tDuration: 487.80 ms\tBilled Duration: 500 ms \tMemory Size: 128 MB\tMax Memory Used: 39 MB\t', 'ingestionTime': 1526375668364, 'eventId': '34039314521038003195592342836784894909026304004227072004'}, {'logStreamName': '2018/05/15/[$LATEST]e9c838560b4a43a8beab55c09b8cff61', 'timestamp': 1526389097179, 'message': 'START RequestId: a27ee858-583f-11e8-942c-83f12a7709a7 Version: $LATEST', 'ingestionTime': 1526389097176, 'eventId': '34039614330004076976238280940123439283024120673455898624'}, {'logStreamName': '2018/05/15/[$LATEST]e9c838560b4a43a8beab55c09b8cff61', 'timestamp': 1526389097665, 'message': 
       "Ec2 Instances which are stopped:  Instance ID:  i-0ab4e0874254619137 Instance state:  {'Code': 80, 'Name': 'stopped'} Instance type:  t2.micro", 'ingestionTime': 1526389097651, 'eventId': '34039614340842239142724163787484244289861010676797800448'}, {'logStreamName': '2018/05/15/[$LATEST]e9c838560b4a43a8beab55c09b8cff61', 'timestamp': 1526389097864, 'message': 
       "Ec2 Instances which are running:  Instance ID:  i-006690f2546191374r Instance state:  {'Code': 16, 'Name': 'running'} Instance type:  t2.micro", 'ingestionTime': 1526389097851, 'eventId': '34039614345280087437231757792891484413311004850060001280'}, {'logStreamName': '2018/05/15/[$LATEST]e9c838560b4a43a8beab55c09b8cff61', 'timestamp': 1526389097864, 'message': 
       "Ec2 Instances which are running:  Instance ID:  i-0384972254619137r4 Instance state:  {'Code': 16, 'Name': 'running'} Instance type:  t2.micro

Upvotes: 1

Views: 142

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626738

It seems your expected matches are between " and ". That means, ^ (start of string) and $ (end of string anchor) should be replaced with double quotes.

So, you may consider using

/"Ec2 Instances.*?micro"/g

See the regex demo.

Note the use of *? quantifier that matches the least amount of any chars between Ec2 and micro as possible to find a valid match.

Also, I added g global modifier to enable multiple matching in the regex tester. Whatever environment you are using, you should consult the relevant documentation to learn how to match multiple occurrences of a pattern inside the string.

Upvotes: 2

Related Questions