emcrisostomo / fswatch

A cross-platform file change monitor with multiple backends: Apple OS X File System Events, *BSD kqueue, Solaris/Illumos File Events Notification, Linux inotify, Microsoft Windows and a stat()-based backend.
https://emcrisostomo.github.io/fswatch/
GNU General Public License v3.0
4.96k stars 327 forks source link

filter regexp: '?' not working? #274

Open mobileink opened 3 years ago

mobileink commented 3 years ago

Hi,

I need to watch .*\.ml$ and .*\.mli$ files. I think regex `.*.ml.?$" ought to work, but it doesn't, so I need to add two filters. Should '?' work? (This is using the C API of libfswatch.)

Thanks

direvus commented 2 years ago

This works for me:

fswatch -E -i '\.mli?$' -e '.*' .

Note the -E to switch into "extended" regexp mode.

A general tip for writing regexps: putting .* at the start or end of a pattern doesn't change anything, unless you are using a function that specifically anchors at the beginning or end of a string, or only matches against the entire string.

For example, in Python, re.match() only matches at the beginning of a string, but re.search() matches anywhere in the string. Most of the time unless you're told otherwise, a regexp will match anywhere in the string. In fswatch, it definitely matches anywhere in the string.