尝试分割字符串(ruby)

时间:2022-08-22 13:08:20

I'm new to Ruby. I'm trying to make some simple calculator program. So, basically what it's supposed to do is you input something like 4+2 and it outputs 6. Simple, right? So I thought. I'm trying to split a string at all the operation characters, so I made this quick regex thinking it would work. It didn't.

我新Ruby。我想做一个简单的计算器程序。基本上,它要做的就是输入4+2,输出6。简单,是吧?我是这么想的。我尝试在所有的操作字符上分割一个字符串,所以我做了这个快速的regex,认为它可以工作。它没有。

class Calculator 
def add(a,b)
    return a+b
end
def sub(a,b)
    return a-b
end
def div(a,b)
    return a/b
end
def mul(a,b)
    return a*b
end
end

operation = gets.split("[\/\+\-\*]")
print(operation)
sleep()

the sleep() is there to pause the console so I can take a look at my outputs. But right now, it's outputting ["4+2\n"]?? I don't know what I'm doing wrong. I need some help. (it should output ["4","+","2"]). Thanks in advance.

sleep()用于暂停控制台,以便查看输出。但是现在,它输出[4+2\n]?我不知道我做错了什么。我需要一些帮助。(它应该输出(“4”、“+”、“2”)。提前谢谢。

3 个解决方案

#1


1  

For a regular expression you want to use / instead of ":

对于正则表达式,您希望使用/而不是“:

operation = gets.split(/[\/\+\-\*]/) # ["4", "2\n"]

操作= gets.split(/[\ / \ + \ \ *]/)#(“4”,“2 \ n”)

There's an extra newline remove that with strip:

有一个额外的换行带:

operation = gets.strip.split(/[\/\+\-\*]/) # ["4", "2"]

操作= gets.strip.split(/[\ / \ + \ \ *]/)#(“4”,“2”)

But we lost the operator. Looking at the docs for split:

但是我们失去了接线员。查看文件分割:

If pattern contains groups, the respective matches will be returned in the array as well.

如果模式包含组,那么相应的匹配也将在数组中返回。

Adding () in our regex creates a group. So then we have:

在regex中添加()将创建一个组。然后我们有:

operation = gets.strip.split(/([\/\+\-\*])/) # ["4", "+", "2"]

操作= gets.strip.split(/((\ / \ + \ \ *))/)#(“4”、“+”、“2”)

#2


2  

You don't need to escape every character inside of your character class. Consider the following:

您不需要转义字符类中的每个字符。考虑以下:

operation = gets.gsub(/[^\d\/*+-]/, '').split(/([\/*+-])/)

or

operation = gets.split(/([\/*+-])/).map(&:strip)

#3


0  

You can try the following code.

您可以尝试以下代码。

operation = gets.chomp.gsub(/[\/\+\-\*]/, ';\0;').split(";")
print(operation)

Execution result:

执行结果:

4+2/3*9-0
["4", "+", "2", "/", "3", "*", "9", "-", "0"]

#1


1  

For a regular expression you want to use / instead of ":

对于正则表达式,您希望使用/而不是“:

operation = gets.split(/[\/\+\-\*]/) # ["4", "2\n"]

操作= gets.split(/[\ / \ + \ \ *]/)#(“4”,“2 \ n”)

There's an extra newline remove that with strip:

有一个额外的换行带:

operation = gets.strip.split(/[\/\+\-\*]/) # ["4", "2"]

操作= gets.strip.split(/[\ / \ + \ \ *]/)#(“4”,“2”)

But we lost the operator. Looking at the docs for split:

但是我们失去了接线员。查看文件分割:

If pattern contains groups, the respective matches will be returned in the array as well.

如果模式包含组,那么相应的匹配也将在数组中返回。

Adding () in our regex creates a group. So then we have:

在regex中添加()将创建一个组。然后我们有:

operation = gets.strip.split(/([\/\+\-\*])/) # ["4", "+", "2"]

操作= gets.strip.split(/((\ / \ + \ \ *))/)#(“4”、“+”、“2”)

#2


2  

You don't need to escape every character inside of your character class. Consider the following:

您不需要转义字符类中的每个字符。考虑以下:

operation = gets.gsub(/[^\d\/*+-]/, '').split(/([\/*+-])/)

or

operation = gets.split(/([\/*+-])/).map(&:strip)

#3


0  

You can try the following code.

您可以尝试以下代码。

operation = gets.chomp.gsub(/[\/\+\-\*]/, ';\0;').split(";")
print(operation)

Execution result:

执行结果:

4+2/3*9-0
["4", "+", "2", "/", "3", "*", "9", "-", "0"]