Xiao
December 31, 2021, 4:07am
1
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!
Matthew
December 31, 2021, 4:46am
2
replace("abc12 3abc ", r"[a-z]+| " => "")
Xiao
December 31, 2021, 4:55am
3
if there’re others in the string, how can do it? Thanks!
julia> replace("abc12 3abc @-", r"[a-z]+| " => "")
"123@-"
Matthew
December 31, 2021, 5:05am
4
replace(str, r"[a-z]+|@|-| " => "")
Should work, though there may be a better method. It’s probably worth messing around with the regex patterns!
Xiao
December 31, 2021, 5:16am
5
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’.
lbilli
December 31, 2021, 5:28am
6
You can try:
julia> replace("abc12 3abc @-", r"[^\d]" => "")
"123"
3 Likes