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
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
Use non greedy +?
/a.+?T/
Test
preg_match('/a.+?T/', 'a111Ta222T', $matches);
echo $matches[0]
=> a111T
^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
.
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