Remove unmatched parts via regular expression

Hi,
For example as below, how can I get the result “123” without “abc, space, …etc.”, using regular expression?

julia> replace("abc12  3abc ", r"\d+" => "")
"abc abc "

Technically, I can meet the aim via filter as below:

julia> filter(isdigit, "abc12  3abc ")
"123"

Just wonder if I can do it via regular expression. Thank you!

replace("abc12 3abc ", r"[a-z]+| " => "")

if there’re others in the string, how can do it? Thanks!

julia> replace("abc12 3abc @-", r"[a-z]+| " => "")
"123@-"

replace(str, r"[a-z]+|@|-| " => "")

Should work, though there may be a better method. It’s probably worth messing around with the regex patterns!

Thanks. Agree that there should have a better way to remove anything (e.g., a-z, @, -, #, …, etc) except a matched pattern. I guess something like ‘!’ or ‘not’.

You can try:

julia> replace("abc12 3abc @-", r"[^\d]" => "")
"123"
3 Likes

that’s it. :+1: