如何确保正则表达式两次匹配?

时间:2013-07-19 19:17:38

标签: php regex

我目前使用以下正则表达式(在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)

基本上,它工作正常,但由于(。*),它匹配第一项和第二项的内容。我试过了很多东西,但是我无法让它将以下字符串识别为两个匹配而不是一个。

----
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.

----

有人知道如何实现这一目标吗?感谢。

更新:请原谅我不够清楚。我想匹配Markdown水平分隔符(----)之间的所有内容,以便形成两个块,这两个都是API方法描述。

1 个答案:

答案 0 :(得分:2)

尝试通过替换:

使其变得非贪婪
.*

.*?

更新:再次检查你的正则表达式后,我不得不再做一些更正,现在跟随我的工作:

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