tschoffelen
tschoffelen

Reputation: 520

How do I make sure regular expression is matched both times?

I currently use the following regex (in PHP):

preg_match_all('/\s+?[-]{3,}\s(POST|GET|PUT|DELETE)\s([\/A-Za-z\-_{}]*)\s+(.*)\s+[-]{3,}\s+/ism',$text, $m, PREG_SET_ORDER)

Basically, it works fine, but due to the (.*), it matches both the contents of the first item, and the second. I tried multiple things, but I can't get it to recognize the following string as two matches instead of one.

----
POST /users/{user-id}/relationship

Modify the relationship between the current user and the target user. 
*Required scope: relationships*

### Parameters
- access_token: A valid access token.
- action: One of follow/unfollow/block/unblock/approve/deny.

### Example response
```
    {
        "meta": {
            "code": 200
        }, 
        "data": {
            "outgoing_status": "requested"
        }
    }
```

----

----
GET /users/{user-id}/relationship

Modify the relationship between the current user and the target user. 
*Required scope: relationships*

### Parameters
- access_token: A valid access token.

----

Someone any idea how to achieve this? Thanks.

Update: Excuse me for not being clear enough. I want to match everything inbetween the Markdown horizontal dividers (----), so that those form two blocks, with are both API method descriptions.

Upvotes: 1

Views: 65

Answers (1)

anubhava
anubhava

Reputation: 785691

Try making it non-greedy by replacing:

.*

To

.*?

Update: After examining your regex again I had to make few more corrections and following now works for me:

preg_match_all('#\s*-{3,}\s(?:POST|GET|PUT|DELETE)\s(?:[\w/{}-]*)\s+(.*?)\s+-{3,}\s*#is',
                $str, $m, PREG_SET_ORDER);

Upvotes: 2

Related Questions