regex - DataArray 不区分大小写的匹配,返回匹配的索引值

标签 regex grep match julia case-insensitive

我的函数内部有一个 DataFrame:

using DataFrames

myservs = DataFrame(serverName = ["elmo", "bigBird", "Oscar", "gRover", "BERT"],
                    ipAddress = ["12.345.6.7", "12.345.6.8", "12.345.6.9", "12.345.6.10", "12.345.6.11"])
myservs
5x2 DataFrame
| Row | serverName | ipAddress     |
|-----|------------|---------------|
| 1   | "elmo"     | "12.345.6.7"  |
| 2   | "bigBird"  | "12.345.6.8"  |
| 3   | "Oscar"    | "12.345.6.9"  |
| 4   | "gRover"   | "12.345.6.10" |
| 5   | "BERT"     | "12.345.6.11" |

如何编写函数来获取名为 server 的单个参数,不区分大小写,与 myservs[:serverName] 中的 server 参数匹配 code> DataArray,并返回匹配对应的ipAddress?

在 R 中,这可以通过使用来完成

myservs$ipAddress[grep("server", myservs$serverName, ignore.case = T)]

我不希望有人使用 ElMoElmo 作为 服务器,或者 serverName 保存为 elmoELMO

最佳答案

我引用了如何在 R 中完成任务并尝试使用 DataFrames pkg 来完成此任务,但我这样做只是因为我来自 R 并且我刚刚学习Julia。我向同事询问了很多问题,以下是我们得出的结论:

This task is much cleaner if I was to stop thinking in terms of vectors in R. Julia runs plenty fast iterating through a loop.

Even still, looping wouldn't be the best solution here. I was told to look into Dicts (check here for an example). Dict(), zip(), haskey(), and get() blew my mind. These have many applications.

My solution doesn't even need to use the DataFrames pkg, but instead uses Julia's Matrix and Array data representations. By using let we keep the global environment clutter free and the server name/ip list stays hidden from view to those who are only running the function.

In the sample code, I'm recreating the server matrix every time, but in reality/practice I'll have a permission restricted delimited file that gets read every time. This is OK for now since the delimited files are small, but this may not be efficient or the best way to do it.

# ONLY ALLOW THE FUNCTION TO BE SEEN IN THE GLOBAL ENVIRONMENT
let global myIP

  # SERVER MATRIX
  myservers = ["elmo" "12.345.6.7"; "bigBird" "12.345.6.8";
               "Oscar" "12.345.6.9"; "gRover" "12.345.6.10";
               "BERT" "12.345.6.11"]

  # SERVER DICT
  servDict = Dict(zip(pmap(lowercase, myservers[:, 1]), myservers[:, 2]))

  # GET SERVER IP FUNCTION: INPUT = SERVER NAME; OUTPUT = IP ADDRESS
  function myIP(servername)
    sn = lowercase(servername)
    get(servDict, sn, "That name isn't in the server list.")
  end
end

​# Test it out
myIP("SLIMEY")
​#>​"That name isn't in the server list."

myIP("elMo"​)
#>​"12.345.6.7"

关于regex - DataArray 不区分大小写的匹配,返回匹配的索引值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29426542/

相关文章:

javascript - 如何使正则表达式变为非贪婪?

linux - 如何重定向在运行 'tail -f' 和 'grep' 的终端中键入的输入?

unix - 如何删除匹配的行和上一行?

windows - grep 这个,但不是这个(windows)

PHP - MySQL 搜索数据库表返回结果与百分比匹配

Python:将员工姓名与公司、年龄、性别相匹配

regex - AWK 中的正则表达式

php - 检查字符串是否只包含php中的数字

java - 如何处理正则表达式中的多个括号?

ElasticSearch:如何在一个或多个索引中跨所有类型搜索任何字段中的值?