简体   繁体   English

RegEx用于匹配行是否在Julia中包含特殊字符

[英]RegEx for matching if a line contains special characters in Julia

file app_ids.txt is of the following format: 文件app_ids.txt具有以下格式:

app1 = "0123456789"
app2 = "1234567890"
app3 = "2345678901"
app4 = "3456789012"
app5 = "4567890123"

printing the lines containing the given regex with the following code in file, find_app_id.jl: 使用以下代码在文件find_app_id.jl中打印包含给定正则表达式的行:

#! /opt/julia/julia-1.1.0/bin/julia

function find_app_id()
   app_pattern = "r\"app2.*\"i"
    open("/path/to/app_ids.txt", "r") do apps
        for app in eachline(apps)
            if occursin(app_pattern, app)
                println(app)
            end
         end
    end
end
find_app_id()

$/home/julia/find_app_id.jl, does not print the second line though it contains the regex! $ / home / julia / find_app_id.jl,尽管包含正则表达式,但不打印第二行!

How do I solve this problem? 我该如何解决这个问题?

Your regular expression looks odd. 您的正则表达式看起来很奇怪。 If you change the line which assigns to app_pattern to 如果将分配给app_pattern的行更改为

app_pattern = r"app2.*"

it should work better. 它应该更好地工作。

For example, the following prints "Found it" when run: 例如,以下代码在运行时显示“找到它”:

app_pattern = r"app2.*"
if occursin(app_pattern, "app2 = blah-blah-blah")
  println("Found it")
else
  println("Nothing there")
end

Best of luck. 祝你好运。

I'm not sure, how regex matching works in Julia, this post might help you to figure it out. 我不确定regex匹配在Julia中如何工作, 这篇文章可能会帮助您弄清楚。

However, in general, your pattern is quite simple, and you probably do not need regular expression matching to do this task. 但是,通常,您的模式非常简单,并且您可能不需要正则表达式匹配即可完成此任务。

This RegEx might help you to design your expression. 此RegEx可以帮助您设计表达。

^app[0-9]+\s=\s\x22([0-9]+)\x22$

There is a simple ([0-9]+) in the middle where your desired app ids are, and you can simply call them using $1 : 中间有一个简单的([0-9]+) ,您可以使用所需的应用程序ID,您可以使用$1对其进行调用:

在此处输入图片说明

This graph shows how the expression would work: 此图显示了表达式如何工作:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM