0

Is there a possibility to use Regex for finding matching string between two dots?

I have strings with direcotries and I need to find string between two dots. Eg: $string = '/Folder/file.co.txt'; and regex will return ONLY co between two dots. I've tried following pattern: '/..../', but it returned .co. with dots.

Is there a possibility to do this with regex or all I can do is splice returned string?

fabtosz
  • 197
  • 3
  • 15

2 Answers2

0

If you use preg_match you can set braces like () to define a group. Your Statement can look like

"~\.(..)\.~g"
episch
  • 388
  • 2
  • 19
0

You could use lookaround:

$string = '/Folder/file.co.txt';
preg_match('/(?<=\.)..(?=\.)/', $string, $matches);
echo $matches[0];

Output:

co
Toto
  • 89,455
  • 62
  • 89
  • 125