admin管理员组

文章数量:1431696

Ultimately I want to match properties of the form: header-args: :tangle <any path>. However, 1) The presence of - results in no output (I guess - is interpreted as "exclude") 2) I have not found a valid way to express <any path>. These correspond to MWEs labeled blah and qux, and, for comparison, bar and foo, respectively.

* match

Matches `bar` as expected
#+begin_src emacs-lisp
  (-match-sparse-tree nil "prefix_key=\"value\"")
#+end_src

Fails to match `blah`
#+begin_src emacs-lisp
  (-match-sparse-tree nil "prefix-key=\"value\"")
#+end_src

Matches `foo` as expected
#+begin_src emacs-lisp
  (-match-sparse-tree nil "key=\"value\"")
#+end_src

Fails to match `qux` 
#+begin_src emacs-lisp
  (-match-sparse-tree nil "key=\"prefix.*\"")
#+end_src

* sample
** bar
:PROPERTIES:
:prefix_key: value
:END:

** blah
:PROPERTIES:
:prefix-key: value
:END:

** foo
:PROPERTIES:
:key: value
:END:

** qux
:PROPERTIES:
:key: prefix value
:END:

UPDATE:

Escaping does not solve the blah case:

* match
** blah 

Fails to match `blah`
#+begin_src emacs-lisp
  (-match-sparse-tree nil "prefix\\-key=\"value\"")
#+end_src

#+RESULTS:

* sample
** blah
:PROPERTIES:
:prefix-key: value
:END:

Other:

  • Matching tags and properties

Ultimately I want to match properties of the form: header-args: :tangle <any path>. However, 1) The presence of - results in no output (I guess - is interpreted as "exclude") 2) I have not found a valid way to express <any path>. These correspond to MWEs labeled blah and qux, and, for comparison, bar and foo, respectively.

* match

Matches `bar` as expected
#+begin_src emacs-lisp
  (-match-sparse-tree nil "prefix_key=\"value\"")
#+end_src

Fails to match `blah`
#+begin_src emacs-lisp
  (-match-sparse-tree nil "prefix-key=\"value\"")
#+end_src

Matches `foo` as expected
#+begin_src emacs-lisp
  (-match-sparse-tree nil "key=\"value\"")
#+end_src

Fails to match `qux` 
#+begin_src emacs-lisp
  (-match-sparse-tree nil "key=\"prefix.*\"")
#+end_src

* sample
** bar
:PROPERTIES:
:prefix_key: value
:END:

** blah
:PROPERTIES:
:prefix-key: value
:END:

** foo
:PROPERTIES:
:key: value
:END:

** qux
:PROPERTIES:
:key: prefix value
:END:

UPDATE:

Escaping does not solve the blah case:

* match
** blah 

Fails to match `blah`
#+begin_src emacs-lisp
  (-match-sparse-tree nil "prefix\\-key=\"value\"")
#+end_src

#+RESULTS:

* sample
** blah
:PROPERTIES:
:prefix-key: value
:END:

Other:

  • Matching tags and properties
Share Improve this question edited Nov 23, 2024 at 3:53 Erwann asked Nov 19, 2024 at 4:52 ErwannErwann 1151 silver badge8 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

For prefix-key, you need to escape the - because otherwise it is interpreted as a negation. So try

#+begin_src emacs-lisp
  (-match-sparse-tree nil "prefix\\-key=\"value\"")
#+end_src

That matches blah.

For regexp matching, the syntax described in the Matching tags and properties that you linked to is {regexp}. So try:

#+begin_src emacs-lisp
  (-match-sparse-tree nil "key={prefix.*}")
#+end_src

That matches qux.

The syntax is sufficiently baroque to require multiple readings plus trial and error, but you usually can come up with an expression that works after a while.

EDIT: the OP reports success for qux but failure for blah. However, at least in my version of Org mode (Org mode version 9.8-pre (release_9.7.15-163-g3ff21c)), blah is matched successfully. See the following screenshot, which I got after pressing C-C C-c on the first code block above (the second code block in the file shown in the screenshot):

Merely a substitute approach using -occur that works (in spite of a wrong type argument message).

.

* code

[same as in the original]

Matches baz as expected.
#+begin_src emacs-lisp
 (-occur ":header-args: :tangle.+$")
#+end_src


* sample

[same as in the original]

** baz
:PROPERTIES:
:header-args: :tangle "/path"
:END:

*Messages*

Executing Emacs-Lisp code block at position 513...
-babel-execute-src-block: Wrong type argument: integer-or-marker-p, nil

本文标签: emacsFailing to match properties containing hyphens using orgmatchsparsetreeStack Overflow