Discussion:
Problems with regex
David, Patrick (I/EG-711)
2009-03-03 15:55:05 UTC
Permalink
Hello out there,

I want to extend my macro library to make nedit able to deal with *.inp
Files (this extension is used by the commercial Finite-Element-Method
software Abaqus).
First I want to teach nedit how to deal with comments in these files. A
commented line starts with two asterisks (**). My macro line for
automatic commenting is the following:

replace_in_selection("^.*$", "** $", "regex")

which seems to work. My problem: how can I program a macro to remove the
first 2 asterisks in a line?
My first shot

replace_in_selection("(^[ \\t]*[\*\*] ?)(.*$)", "\\2", "regex")

doesn't work. I don't have experiences with this function (what does
"\\2" mean)?

In addition it's probably important to know that keywords in Abaqus
starts with a single asterisk. Example:

Original:
----------------------
*STEP, NAME=myStep
My Step Description
*STATIC
*LOAD
1, 1
*END STEP

Commented:
-------------------------
***STEP, NAME=myStep
**My Step Description
***STATIC
***LOAD
**1, 1
***END STEP

Has anyone of you an idea for the uncommenting macro function?
Hope for help
Patrick
--
NEdit Discuss mailing list - Discuss-***@public.gmane.org
http://www.nedit.org/mailman/listinfo/discuss
Joerg Fischer
2009-03-03 17:42:40 UTC
Permalink
Post by David, Patrick (I/EG-711)
which seems to work. My problem: how can I program a macro to remove the
first 2 asterisks in a line?
I think the find pattern is ^\s*\*\*, which is to be replaced with an
empty string.
Post by David, Patrick (I/EG-711)
My first shot
replace_in_selection("(^[ \\t]*[\*\*] ?)(.*$)", "\\2", "regex")
doesn't work. I don't have experiences with this function (what does
"\\2" mean)?
\2 is matched string of the second parentheses, here .*$, which is the
rest of the line after the **. If you don't put the \* inside a
character class (inside the brackets) it will work.

Btw, macros can also be recorded. So, you could use the replace
dialog to type in the pattern (with less escaping) and record
and save the action. It's all pretty well documented in the online
help.

--Jörg
--
NEdit Discuss mailing list - Discuss-***@public.gmane.org
http://www.nedit.org/mailman/listinfo/discuss
Tony Balinski
2009-03-05 21:41:30 UTC
Permalink
Post by David, Patrick (I/EG-711)
Hello out there,
I want to extend my macro library to make nedit able to deal with *.inp
Files (this extension is used by the commercial Finite-Element-Method
software Abaqus).
First I want to teach nedit how to deal with comments in these files. A
commented line starts with two asterisks (**). My macro line for
replace_in_selection("^.*$", "** $", "regex")
This deletes what was on the line already. Use "** &" as the replace string to
keep that.
Post by David, Patrick (I/EG-711)
which seems to work. My problem: how can I program a macro to remove the
first 2 asterisks in a line?
My first shot
replace_in_selection("(^[ \\t]*[\*\*] ?)(.*$)", "\\2", "regex")
doesn't work. I don't have experiences with this function (what does
"\\2" mean)?
What you'd enter into the Replace/Find dialog with a backslash (\), you
must type with a double backslash in a nedit macro string. That's why you
use "\\2" to pick up the text of group 2 in your replacement (that which
matches the part of your pattern in the second set of parentheses). It's
up to you whether to use "\\t" instead of "\t" in the case of regex
interpretation: the first is passed as \t to the regex engine (where it's
treated as indicating a tab character) , the second passes a tab in the
string. As for "[ \\t]", you can write "\\s" for "match a space, tab or other
space-like character".

Now your "[\*\*]" means the same as "[**]" (since the sequence \* doesn't
mean anything special in a macro string, unlike \t or \n). In a regex, the
square brackets form a character class: a single character must match one of
the characters in the class. You have the same character twice: a *. What you
want is to recognise two in sequence. You could write [*][*] - a bit heavy
duty. Instead you want to match **, but since * is a regex special character,
you need to escape with backslashes, which need to be doubled in the macro
string. This makes your search pattern "(^[ \\t]*\\*\\* ?)(.*$)". But you can
do without the groups: try
replace_in_selection("^\\s*\\*\\* ?", "", "regex")
which doesn't bother with replacing the "rest of line" group 2, just replacing
the front of the line. But this loses any indentation in front of the **, so
you could save those spaces at the front into group 1 (and forgetting the
actual ** sequence):
replace_in_selection("^(\\s*)\\*\\* ?", "\\1", "regex")
Post by David, Patrick (I/EG-711)
--
http://www.nedit.org/mailman/listinfo/discuss
--
NEdit Discuss mailing list - Discuss-***@public.gmane.org
http://www.nedit.org/mailman/listinfo/discuss
Continue reading on narkive:
Loading...