0

I would like to know how to match a string with a regular expression for a certain length from a certain character or substring.

For example how do i match 5 characters back from the "tk" substring:

The string: "45gtyr eret dffg 45234tk ssdfr"

Should return: "45234".

1 Answer 1

3

Use a positive lookahead or capturing group.

.{5}(?=tk)

DEMO

OR

(.{5})tk

DEMO

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

2 Comments

The first one does work great, the second one return "tk" as well as the 5 characters before it, But thank you very much! that helped me a lot :)
@RealGigex print the group index 1 in the second instead of index 0. Since you're new to stackoverflow, i suggest you to read this

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.