1

I am new in regular expression and I want to know if following is possible to done with it.

I want to match string with provided positions.

For example

$input = 'text example'.

I want to match the letters from 4th to 7th, that is

"t ex".

In this case, how can I write the regex?

I tried below

'/^.{4,7}.*$/e'

its with e filter because I want to use function of it, but this way is not the result I want...

Thank you for your help!

1
  • 2
    you may use substring for this. Why you need regex? Commented Sep 23, 2015 at 18:01

1 Answer 1

2

You may use substring for this. If you really want to use regex then try this,

preg_match('~^.{3}\K.{4}~', $str);

\K discards previously matched characters from printing at the final. You may also use positive look-behind instead of \K.

DEMO

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

2 Comments

Thank you very much!, I think this is what I want, I will accept this when time limit passed.
Can I ask one more question? if I want to have 4th and 7th only not in range, how should I change the pattern?

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.