Remove unmatched parts via regular expression

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 ")

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]+| " => "")

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]" => "")

that’s it. :+1: