如何使用正则表达式拆分字符串?

时间:2015-08-18 08:14:34

标签: regex string express

我想知道如何使用正则表达式分割字符串。

e.g

我的字符串在下面。

String Temp = "/dev/sda        398G  4.9G  373G   2% /mnt/internal";

我想将上面的字符串与组名和值分开。

(组名)=(值)

filestream = / dev / sda

尺寸= 398G

使用= 5.8G

保持= 372G

使用%= 2%

mount = / mnt / internal

如果有人知道如何将上述字符串转换为正则表达式,请留下答案。

事先感谢您的回答。

每个人都过得愉快!

1 个答案:

答案 0 :(得分:1)

没有"命名组"在Javascript RegEx afaik。 但你也可以用#34; normal"组。这是正则表达式:

([\w\d/]+)\s+([\d\.G]+)?\s+([\d\.G]+)?\s+([\d\.G]+)?\s+(\d+%)\s+([\w\d/]+)

详细说明:

([\w\d/]+)       -> match group of "chars", "digits" and "/"
\s+              -> followed by at least on whitespace
([\d\.G]+)?      -> match shortest match of "digits", "." and "/" and char "G"
\s+              -> followed by at least on whitespace
([\d\.G]+)?      -> match shortest match of "digits", "." and char "G"
\s+              -> followed by at least on whitespace
([\d\.G]+)?      -> match shortest match of "digits", "." and char "G"
\s+              -> followed by at least on whitespace
(\d+%)           -> match "digits" followed by "%"
\s+              -> followed by at least on whitespace
([\w\d/]+)       -> match group of "digits", "chars" and "/"

之后,您可以访问与您需要的组相对应的组1到6。

http://regexr.com/3bnsd