AVA
AVA

Reputation: 2558

RegEx for matching if a line contains special characters in Julia

file app_ids.txt is of the following format:

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:

#! /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!

How do I solve this problem?

Upvotes: 0

Views: 205

Answers (2)

Your regular expression looks odd. If you change the line which assigns to app_pattern to

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.

Upvotes: 2

Emma
Emma

Reputation: 27723

I'm not sure, how regex matching works in Julia, this post might help you to figure it out.

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.

^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:

enter image description here

This graph shows how the expression would work:

enter image description here

Upvotes: 1

Related Questions