Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ruby中匹配模式后提取数据_Ruby - Fatal编程技术网

在ruby中匹配模式后提取数据

在ruby中匹配模式后提取数据,ruby,Ruby,放入我的值[1](我的值是字符串) 我试图在掷骰后提取并分配给数组 groups = myvalue[1].match(/^[Roll].*/) 我得到的输出 new line = see that cfiu\poli 而不是(预期的) 如何操作,谢谢更改您的组将行分配给此: groups = myvalue[1].partition(/\[Roll\]/).last 更改您的组将行分配给此: groups = myvalue[1].partition(/\[Roll\]/).last

放入我的值[1]
(我的值是字符串)

我试图在掷骰后提取并分配给数组

groups = myvalue[1].match(/^[Roll].*/)
我得到的输出

new line = see that cfiu\poli
而不是(预期的)


如何操作,谢谢

更改您的组将行分配给此:

groups = myvalue[1].partition(/\[Roll\]/).last

更改您的组将行分配给此:

groups = myvalue[1].partition(/\[Roll\]/).last
更新: 更新:
我无法复制你得到的输出。你能举个完整的例子吗?我假设您想匹配“所有不以“[Roll]”开头的行”,对吗?那将是
^(?!\[Roll\]).*/mg
。您当前的正则表达式与以“R”、“o”或“l”开头的第一行匹配。我无法重现您得到的输出。你能举个完整的例子吗?我假设您想匹配“所有不以“[Roll]”开头的行”,对吗?那将是
^(?!\[Roll\]).*/mg
。您当前的正则表达式匹配以“R”、“o”或“l”开头的第一行。感谢分区方法感谢分区方法感谢这一技巧。很抱歉,这是对我的问题的补充。你知道如何根据分隔符=进行拆分,并在
first\u arr=[“新行”、“check\u that”、“stares”]
second\u arr=[其相应的拆分值]
@voltas
first\u arr=split(=”[0])下面以数组的形式获取值吗;第二个+arr=拆分(“=”[1]
@RamonMarques:谢谢你的建议,但是我只得到了第一个值,即
新行
而不是
[“新行”,“检查它”,“凝视”]
在打印第二个数组时,我得到了“这是新行内容检查”`@voltas实际上提出了另一个问题似乎你在试图解析INI文件。你可以用图书馆来做这件事:谢谢你,这就成功了。很抱歉,这是对我的问题的补充。你知道如何根据分隔符=进行拆分,并在
first\u arr=[“新行”、“check\u that”、“stares”]
second\u arr=[其相应的拆分值]
@voltas
first\u arr=split(=”[0])下面以数组的形式获取值吗;第二个+arr=拆分(“=”[1]
@RamonMarques:谢谢你的建议,但是我只得到了第一个值,即
新行
而不是
[“新行”,“检查它”,“凝视”]
在打印第二个数组时,我得到了“这是新行内容检查”`@voltas实际上提出了另一个问题似乎你在试图解析INI文件。您可以使用一个库:
groups = myvalue[1].partition(/\[Roll\]/).last
> string = "String begins here
[Roll]
new line = this is new line content
check_that = check that line content
stares = stares content"

> required_string = string.split("[Roll]").last.strip
# new line = this is new line content
# check_that = check that line content
# stares = stares content
> first_arry = required_string.split("\n").map{|e| e.split("=")}.map(&:first)
#=> ["new line ", "check_that ", "stares "] 
> second_arry = required_string.split("\n").map{|e| e.split("=")}.map(&:last)
#=> [" this is new line content", " check that line content", " stares content"]