user1016837
user1016837

Reputation: 9

Need to find expression and grab next 3 characters using regex

Would prefer if there is a way to use a regular expression to do this.

What I need to do is take a string such as "abc123xyz" search for "abc" then grab the next 3 characters which would be "123".

Any thoughts?

Thanks

Upvotes: 1

Views: 70

Answers (2)

xanatos
xanatos

Reputation: 111850

(?<=abc)(.{3})

This will capture any three characters following abc. (?<=abc) is a lookbehind expression. Note that not all the regex engines support lookbehind expressions.

Upvotes: 2

Marcus
Marcus

Reputation: 12586

This regex would capture the three following characters after abc:

^abc(.{3})

If abc is not positioned in the start of the string simply remove the ^-character which indicates start of string.

Upvotes: 1

Related Questions