F21
F21

Reputation: 33391

regex match as 1 unit

I am using the PHP to run a regex on some strings.

The strings looks like:

somethingsomethin.somethingsomething.extension

I want to match the bits between the 2 periods and including the 2 periods part in the above:

.somethingsomething.

I came up with something simple like: \..+\.

The problem is that it matches all the periods in something like this:

somethingsomethin....somethingsomething....extension matches as ....somethingsomething.... when I only want .somethingsomething..

How can I get my regex expression to match as "1 unit" and to match only once?

Upvotes: 0

Views: 47

Answers (2)

Roman Newaza
Roman Newaza

Reputation: 11690

The . matches entire string oin your example. Try this:

<?php

$str = 'somethingsomethin....somethingsomething....extension';

preg_match('#\.\w+\.#', $str, $m);

print_r($m);

Upvotes: 0

deceze
deceze

Reputation: 522076

Since . matches a ., exclude literal .s: \.[^.]+\. or possibly \.\w+\..

Upvotes: 2

Related Questions