I'm trying to match the following three lines:
usemtl ftw
kd 1.2 3.2 3.1
v -12.1892 -53.4267 -276.4055
My regex matches the first two:
^(\w+) ((\S+)( \S+)*) *$
I've tried a few variants to match the negative numbers, but they just stop anything from being matched:
^(\w+) (([\S-]+)( [\S-]+)*) *$
^(\w+) (((\S|-)+)( (\S|-)+)*) *$
What am I supposed to do here? -
isn't a special character in regex, is it?
-
is only a special character in character classes [...]
Your problem comes from v -12.1892 -53.4267 -276.4055
containing 2 spaces in between v
and -12.18...
. Your regex only matches one.
Try this regex instead:
^(\w+)\s*((\S+)( \S+)*) *$
Although your regex could be simplified to (not sure exactly what you want to match and capture though):
^(\w+)(\s*\S+)*$
See it on http://rubular.com/r/D86njdYzJF
Put it first in the class: [-\\S]
Then it should work.
There are two spaces between v and -12.1892 that seems to be your problem. Also to use -
inside a character class ie [...]
you need to escape it with \\-
The reason why it isn't matching is because your third line has two spaces between the v
and -12.1892
. Try this:
^(\\w+) +(([\\S]+)( [\\S]+)*) *$
(the added +
sign allows for multiple spaces)
Here is the jsfiddle to test it: http://jsfiddle.net/xewys/
我能想到的与您的示例数据相匹配的最基本的正则表达式是“(\\S+\\s+)+”,但这可能不适合您 - 它似乎太通用了。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.