Page 1 of 1

Regex Assistance (not TextPad)

Posted: Mon Aug 18, 2008 7:42 pm
by SteveH
I could do with a bit of assistance finishing off a couple or regular expressions for a syntax definition file.

Code: Select all

- DRI Updates @today @done
In this example I want to match the whole line, up to but not including the first @ symbol. I've been using the following regex which is close, works on lines with only a single @ character but when there are two or more the look ahead assertion will capture up till the last one.

Code: Select all

^[\t ]*- .*(?=@)
Tabs or space are allowed before the all-important '-' character.

Similarly comments are defined by a line that does not start with the '-' character and does not end with a ':'. Again, tabs and spaces are allowed at the start of the line and at the end. The regex I have been using is shown below. The problem is that while this is capturing comments it's also capturing lines with colons at the end of the line.

Code: Select all

^[\t ]*(?!-).*(?!:)
As an aside it would be fantastic if TextPad supported syntax definition via regex - it's so straightforward, despite these kind of problems.

Posted: Mon Aug 18, 2008 10:27 pm
by ben_josephs
1.
^[\t ]*(?!-).*(?!:)
works in WildEdit.

2.
^[\t ]*[^-].*[^:][\t ]*$
works in TextPad and WildEdit.

Posted: Tue Aug 19, 2008 5:27 pm
by SteveH
Unfortunately while these both capture the required strings (comments) they also capture projects, which are of the form:

Code: Select all

Project:
The second one only capture projects with a space after the colon.

I presume this is happening because the .* element is being greedy and capturing the colon too. I've not had any time to play around with this today so may try again tonight. I may create a more detailed example showing each of the elements.

Posted: Tue Aug 19, 2008 7:15 pm
by ben_josephs
That's right.

You specified that a comment is a line that does not start with the '-' character and does not end with a ':'. A line containing just
Project:
with no spaces at the end does not start with a '-', but does end with a ':'. By your definition, that is not a comment.

Did you mean that a comment is a line that does not start with a '-' or does not end with a ':' ? If so, try
^\s*([^-].*|.*[^:])\s*$
or
^(?!\s*-.*:\s*$).*

Posted: Tue Aug 19, 2008 7:35 pm
by SteveH
I've just spotted that 1224 posts makes you this forum's most prolific poster (and most useful)!

Thanks for all the regex magic.

Posted: Tue Aug 19, 2008 7:57 pm
by ben_josephs
Thank you. You're welcome.

Posted: Tue Aug 19, 2008 9:54 pm
by SteveH
Here's a fuller example of the type of file structure:

Code: Select all

Project:
- Task
- Task 2  @done @today
    Note
	- Task 3 @today
Project2:
- Task4    
The file is a text file associated with a Windows application called ToDoPaper and an OS X application called TaskPaper which use the same file format.

To create a syntax definition file for couple of text editors that use regex syntax definitions (PCRE) I have to define four elements for which the rules are:
  1. A project entry is defined by the colon at the end of the line. The colon can be followed or preceded by spaces and the text may be inset with tabs or spaces.
  2. A task is defined the '-' character being present at the start of the text but may be preceded by tabs or spaces. The entry ends with an end of line or tag.
  3. A tag is defined by starting with a space followed by an '@' character.
  4. A comment string is every thing else i.e. does not start with '-' or end with ':' or follow an '@'.

I'm still pottering about with this but only in a very desultory manner so far.

Posted: Tue Aug 19, 2008 11:37 pm
by ben_josephs
You can do all these even in TextPad:

Code: Select all

Entry:    ^.*:[\t ]*$
Task:     ^[\t ]*-.*$
Comment:  ^[\t ]*[^ -].*[^ :][\t ]*$