Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pulling a substring in Julia

I’m working with XML embedded in a syslog message. I used Python to remove the information outside of the <>. Since I’m playing with Julia I’m trying to figure out a way of doing the same thing. I’ve read about findfirst, but that doesn’t resolve the issue. This is sample data.

Datetime host other stuff <xml data and more data>stuff at the end

What I want is just the data between <>. In Python I use

print(line[line.find(“<“):line.find(“>”)])

Is there anything similar in Julia?

TIA Joe

like image 445
Joe Hughes Avatar asked Aug 12 '26 00:08

Joe Hughes


2 Answers

Alternatively you can use regular a expression:

julia> str = "Datetime host other stuff <xml data and more data>stuff at the end"
"Datetime host other stuff <xml data and more data>stuff at the end"

julia> rx = r"<(.*?)>"
r"<(.*?)>"

julia> match(rx, str)[1]
"xml data and more data"

If you wanted to use the approach that Oscar proposes then the correct syntax would be:

julia> chop(str[findfirst('<',str):findfirst('>',str)], head=1, tail=1)
"xml data and more data"

Finally note that in Python your code does not give you what you want as it produces:

>>> line = "Datetime host other stuff <xml data and more data>stuff at the end"
>>> print(line[line.find("<"):line.find(">")])
<xml data and more data

and as you can see < character is not stripped from the string as you wanted.

like image 90
Bogumił Kamiński Avatar answered Aug 14 '26 15:08

Bogumił Kamiński


Since it is log processing perhaps the performance is somewhat important. In that case use SubString{String} (which does not make memory copying). Moreover you probably want to use findlast when searching for '>'.

SubString(line, findfirst('<', line), findlast('>',line))

This is non-copying and returns a SubString{String} object.

like image 40
Przemyslaw Szufel Avatar answered Aug 14 '26 15:08

Przemyslaw Szufel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!