I have three variations of a string:
我有三个字符串变体:
1. view=(edit:29,30)
2. view=(edit:29,30;)
3. view=(edit:29,30;x:100;y:200)
I need a RegExp that:
我需要一个RegExp:
- capture up to and including ",30"
- capture "x:100;y:200" - whenever there's a semicolon after the first match;
- WILL NOT include leftmost semicolon in any of the groups;
- entire string on the right of the first semicolon and up to ')' can/should be in the same group.
捕获到包括“,30”
捕获“x:100; y:200” - 只要在第一场比赛后有分号;
不包括任何组中最左边的分号;
第一个分号右边的整个字符串和最多')'可以/应该在同一个组中。
I came up with:
我提出了:
$pat = '/view=\((\w+)(:)([\d,]+)((;[^)]+){0,}|;)\)/';
Applied to 'view=(edit:29,30;x:100;y:200)' it yields:
应用于'view =(编辑:29,30; x:100; y:200)'它产生:
Array
(
[0] => view=(edit:29,30;x:100;y:200)
[1] => edit
[2] => :
[3] => 29,30
[4] => ;x:100;y:200
[5] => ;x:100;y:200
)
THE QUESTION. How do I remove ';' from matches [4] and [5]?
问题。如何删除';'来自比赛[4]和[5]?
IMPORTANT. The same RegExp should work with a string when no semicolons are present, as: 'view=(edit:29,30)'.
重要。当没有分号存在时,相同的RegExp应该使用字符串,如:'view =(edit:29,30)'。
$pat = '/view=\((\w+)(:)([\d,]+)((;[^)]+){0,}|;)\)/';
$str = 'view=(edit:29,30;x:100;y:200)';
preg_match($pat, $str, $m);
print_r($m);
Thanks!
2 个解决方案
#1
You don’t need to group everything. Try this regular expression:
您不需要对所有内容进行分组。试试这个正则表达式:
/view=\((\w+):([\d,]+)(?:;([^)]+)?)?\)/
#2
I guess you want something like this:
我想你想要这样的东西:
$pattern = '/view=\\((\\w+):(\\d+,\\d+)(?:;((?:\\w+:\\d+;?)*))?\\)/';
Should return
[0] view=(edit:29,30;x:100;y:200)
[1] edit
[2] 29,30
[3] x:100;y:200
#1
You don’t need to group everything. Try this regular expression:
您不需要对所有内容进行分组。试试这个正则表达式:
/view=\((\w+):([\d,]+)(?:;([^)]+)?)?\)/
#2
I guess you want something like this:
我想你想要这样的东西:
$pattern = '/view=\\((\\w+):(\\d+,\\d+)(?:;((?:\\w+:\\d+;?)*))?\\)/';
Should return
[0] view=(edit:29,30;x:100;y:200)
[1] edit
[2] 29,30
[3] x:100;y:200