简体   繁体   English

如何在Emacs中正则表达式搜索=,而不是> =,==,<=,!=,〜=等?

[英]How to regexp search for =, but not >=, ==, <=, !=, ~=, etc. in Emacs?

I would like to do regexp search for the = character, which may or may not have spaces on either sides, and may be at the beginning or end of a line. 我想对=字符进行正则表达式搜索,该字符在两侧可能有也可能没有空格,并且可能位于一行的开头或结尾。 I've tried a couple: 我尝试了几个:

  • = doesn't work because it matches == , >= , etc. =无效,因为它匹配==>=等。
  • [^<>=]=[^<>=] doesn't work because it captures the two characters immediately before and after the = character, which is undesirable [^<>=]=[^<>=]不起作用,因为它会捕获=字符前后的两个字符,这是不希望的
  • I thought \\_< and \\_> might be useful but I cannot figure out what these are for 我以为\\_<\\_>可能有用,但我无法弄清楚它们的用途

In Vim, there are \\zs and \\ze which denote the start and end of a match so that I can match against the surrounding characters only for context. 在Vim中,有\\zs\\ze表示匹配的开始和结束,因此我只能针对上下文与周围的字符进行匹配。 But Emacs doesn't have these. 但是Emacs没有这些。

Is this possible with Emacs? Emacs有可能吗?

Your question is underspecified. 您的问题未指定。 What characters do you disallow just before and after = ? 您在=之前和之后不允许使用什么字符? You allow space chars (what about other whitespace?). 您允许使用空格字符(其他空格是什么?)。 But do you allow only space chars (or whitespace)? 但是,您允许使用空格字符(或空格)吗?

Eg, do you allow a=b to match? 例如,您允许a=b匹配吗? If so, then perhaps letters are not disallowed? 如果是这样,那么也许不禁止使用字母?

Once you specify clearly what chars you allow and disallow immediately before and after = , you can write the regexp. 一旦明确指定在=之前和之后允许和禁止的字符,就可以编写正则表达式。 But since you want to "capture" only the = in the matching contexts you will need to use a regexp group for the = and retrieve only what it matches, after matching everything. 但是,由于您只想在匹配的上下文中“捕获” = ,因此您需要为=使用一个正则表达式组,并在匹配所有内容后仅检索匹配的内容。

For example, [ ]*=[ ]* matches = possibly surrounded by space chars. 例如, [ ]*=[ ]* matchs =可能被空格字符包围。 But if you want to capture only the = in that context then you need to use [ ]*\\\\(=\\\\)[ ]* and then use (match-string 1) to get the = . 但是,如果您只想在该上下文中捕获= ,则需要使用[ ]*\\\\(=\\\\)[ ]* ,然后使用(match-string 1)获取=

You also don't say whether you are matching text in a buffer or in a string. 您也不会说要匹配缓冲区中的文本还是字符串中的文本。

And you don't say whether you want to do this interactively or from a Lisp program. 而且,您不会说是要交互地进行操作还是要从Lisp程序中进行操作。

The exact code you need depends on these things. 您需要的确切代码取决于这些东西。 If interactively, eg, for search, you don't need to double backslashes. 如果以交互方式(例如,用于搜索),则无需加倍反斜杠。 If a string instead of a buffer then you need to pass the string as a second argument to match-string . 如果使用字符串而不是缓冲区,则需要将字符串作为第二个参数传递给match-string

In general, your question is underspecified, and the answer involves specifying these things - in particular, just what contexts you want to match (what chars are allowed before and after = ) and what part(s) of the matching contexts you want to retrieve (just the = or the = plus some of the chars before and after it). 通常,您的问题未明确指定,答案涉及指定这些内容-特别是,您要匹配的上下文( =和之前和之后允许使用什么字符)以及要检索的匹配上下文的哪些部分(只是==加上前后的一些字符)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM