help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Use regex in ~/.emacs.d/init.el to match no_proxy.


From: Hongyi Zhao
Subject: Re: Use regex in ~/.emacs.d/init.el to match no_proxy.
Date: Sat, 10 Apr 2021 21:49:28 +0800

On Thu, Apr 8, 2021 at 2:29 PM Joost Kremers <joostkremers@fastmail.fm> wrote:
>
>
> On Thu, Apr 08 2021, Hongyi Zhao wrote:
> >> Eshell allows you to leave out the outermost parentheses, so instead
> >> of [1] you can also write
> >>
> >> [2] regexp-opt "*.cn" "localhost" "10.0.0.0/8" "127.0.0.0/8" 
> >> "172.16.0.0/12"
> >> "192.168.0.0/16"
> >
> > Still failed, as shown below:
> >
> > ~ $ regexp-opt "*.cn" "localhost" "10.0.0.0/8" "127.0.0.0/8"
> > "172.16.0.0/12" "192.168.0.0/16"
> > Wrong number of arguments: (1 . 2), 6
>
> regexp-opt takes a list of strings, so you'd need:
>
>     regexp-opt ("*.cn" "localhost" "10.0.0.0/8" "127.0.0.0/8" "172.16.0.0/12" 
> "192.168.0.0/16")
>
> But that doesn't work because the parens are taken as a Lisp expression, so 
> its
> first element needs to be a function. Quoting it doen't work either:
>
>     regexp-opt '("*.cn" "localhost" "10.0.0.0/8" "127.0.0.0/8" 
> "172.16.0.0/12" "192.168.0.0/16")
>
> Because then eshell is waiting for the closing single quote. So just add the
> parens around the entire expression:
>
>     (regexp-opt '("*.cn" "localhost" "10.0.0.0/8" "127.0.0.0/8" 
> "172.16.0.0/12" "192.168.0.0/16"))

Really, it does the trick:

~ $ (regexp-opt '("*.cn" "localhost" "10.0.0.0/8" "127.0.0.0/8"
"172.16.0.0/12" "192.168.0.0/16"))
\(?:\*\.cn\|1\(?:0\.0\.0\.0/8\|27\.0\.0\.0/8\|72\.16\.0\.0/12\|92\.168\.0\.0/16\)\|localhost\)

> For Lisp interaction, I'd recommend `M-x ielm`, though.

This also works like a charm:

ELISP> (regexp-opt '("*.cn" "localhost" "10.0.0.0/8" "127.0.0.0/8"
"172.16.0.0/12" "192.168.0.0/16"))
"\\(?:\\*\\.cn\\|1\\(?:0\\.0\\.0\\.0/8\\|27\\.0\\.0\\.0/8\\|72\\.16\\.0\\.0/12\\|92\\.168\\.0\\.0/16\\)\\|localhost\\)"

But why the 2nd method gives the double '\\' escaped sequence for some
metacharacters while the the 1st method doesn't?

Regards
-- 
Assoc. Prof. Hongyi Zhao <hongyi.zhao@gmail.com>
Theory and Simulation of Materials
Hebei Polytechnic University of Science and Technology engineering
NO. 552 North Gangtie Road, Xingtai, China



reply via email to

[Prev in Thread] Current Thread [Next in Thread]