Search code examples
emacsorg-mode

Failing to match properties containing hyphens using `org-match-sparse-tree`


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
  (org-match-sparse-tree nil "prefix_key=\"value\"")
#+end_src

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

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

Fails to match `qux` 
#+begin_src emacs-lisp
  (org-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
  (org-match-sparse-tree nil "prefix\\-key=\"value\"")
#+end_src

#+RESULTS:

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

Other:


Solution

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

    #+begin_src emacs-lisp
      (org-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
      (org-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):

    enter image description here