0

If I have a111Ta222T

I wish to extract in PHP a111T

The point is to stop match at the first occurence of T, instead of at the last T.

How can I do this using regex?


I don't wish to use a[\S]+(?=a)

which can have the same result a111T

Whats Going On
  • 1,379
  • 6
  • 20
  • 49
Joon. P
  • 2,238
  • 7
  • 26
  • 53

3 Answers3

1

Use non greedy +?

/a.+?T/

Regex Demo

Test

preg_match('/a.+?T/', 'a111Ta222T', $matches);
echo $matches[0]
=> a111T
nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52
0
^a\S+?T

You can simply do this.+? is non greedy and will stop at the first instance of T.You would also want to ^ anchor your string in order to stop at first T.

vks
  • 67,027
  • 10
  • 91
  • 124
0

you can use this regex

[^T]+T

check this Demo

if you want your match to start with letter a you can use a[^T]+T

Nader Hisham
  • 5,214
  • 4
  • 19
  • 35