I would like to get a regexp that will extract out the following. I have a regexp to validate it (I pieced it together, so it may not be the the best or most efficient).
我想获得一个将提取以下内容的正则表达式。我有一个正则表达式来验证它(我把它拼凑在一起,所以它可能不是最好的或最有效的)。
some.text_here:[12,34],[56,78]
The portion before the colon can include a period or underline. The bracketed numbers after the colon are coordinates [x1,y1],[x2,y2]... I only need the numbers from here.
结肠前部分可包括周期或下划线。冒号后括号内的数字是坐标[x1,y1],[x2,y2] ......我只需要这里的数字。
And here is the regexp validator I was using (for javascript):
这是我正在使用的regexp验证器(用于javascript):
^[\w\d\-\_\.]*:(\[\d+,\d+],\[\d+,\d+])
I'm fairly new to regexp but I can't figure out how to extract the values so I can get
我对regexp相当新,但我无法弄清楚如何提取值,以便我可以得到
name = "some.text_here"
x1 = 12
y1 = 34
x2 = 56
y2 = 78
Thanks for any help!
谢谢你的帮助!
3 个解决方案
#1
Try this regular expression:
试试这个正则表达式:
/^([\w\d-_.]*):\[(\d+),(\d+)],\[(\d+),(\d+)]/
var str = "some.text_here:[12,34],[56,78]";
var match = str.match(/^([\w\d-_.]*):\[(\d+),(\d+)],\[(\d+),(\d+)]/);
alert("name = " + match[1] + "\n" +
"x1 = " + match[2] + "\n" +
"x2 = " + match[3] + "\n" +
"y1 = " + match[4] + "\n" +
"y2 = " + match[5]);
#2
You can use the match method of string:
您可以使用string的匹配方法:
var input = "some.text_here:[12,34],[56,78]";
var matches = input.match(/(.*):\[(\d+),(\d+)\],\[(\d+),(\d+)\]/);
var output = {
name: matches[1],
x1: matches[2],
y1: matches[3],
x2: matches[4],
y2: matches[5]
}
// Object name=some.text_here x1=12 y1=34 x2=56 y2=78
#3
You want something like:
你想要的东西:
/^(\S+):\[(\d+),(\d+)\],\[(\d+),(\d+)\]/
I am not sure if JavaScript supports the naming of caputre groups, but if it did, you could add those in as well.
我不确定JavaScript是否支持caputre组的命名,但如果确实如此,你也可以添加它们。
#1
Try this regular expression:
试试这个正则表达式:
/^([\w\d-_.]*):\[(\d+),(\d+)],\[(\d+),(\d+)]/
var str = "some.text_here:[12,34],[56,78]";
var match = str.match(/^([\w\d-_.]*):\[(\d+),(\d+)],\[(\d+),(\d+)]/);
alert("name = " + match[1] + "\n" +
"x1 = " + match[2] + "\n" +
"x2 = " + match[3] + "\n" +
"y1 = " + match[4] + "\n" +
"y2 = " + match[5]);
#2
You can use the match method of string:
您可以使用string的匹配方法:
var input = "some.text_here:[12,34],[56,78]";
var matches = input.match(/(.*):\[(\d+),(\d+)\],\[(\d+),(\d+)\]/);
var output = {
name: matches[1],
x1: matches[2],
y1: matches[3],
x2: matches[4],
y2: matches[5]
}
// Object name=some.text_here x1=12 y1=34 x2=56 y2=78
#3
You want something like:
你想要的东西:
/^(\S+):\[(\d+),(\d+)\],\[(\d+),(\d+)\]/
I am not sure if JavaScript supports the naming of caputre groups, but if it did, you could add those in as well.
我不确定JavaScript是否支持caputre组的命名,但如果确实如此,你也可以添加它们。