1

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

8
  • Are you sure these are separate strings/lines? Remove ^ and $ anchors if not. From your input string, it seems these values are inside double quotes, maybe all you need is replace ^ and $ with " Commented May 16, 2018 at 9:40
  • yes, these lines comes in between my logs, I removed and tried as you said, but it is matching only first occurence Commented May 16, 2018 at 9:42
  • What do you mean? How did you use the regex? Where? What method? Commented May 16, 2018 at 9:45
  • I test it here (regexr.com) Commented May 16, 2018 at 9:46
  • See regexr.com/3phbp, all occurrences are matched. Commented May 16, 2018 at 9:47

1 Answer 1

2

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.

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

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.