Hi everybody!
Is there (in TextMate's built-in search) a solution for forming a replace string for a variable directly followed by a number, ie. $15 (read: $1 and 5, without a space in between)?
Can this somehow be done or is reformulating the search string the only way in such a case?
Thanks!
Chris
On 02.09.2008, at 17:35, Christian Bogen wrote:
Hi everybody!
Is there (in TextMate's built-in search) a solution for forming a replace string for a variable directly followed by a number, ie. $15 (read: $1 and 5, without a space in between)?
Can this somehow be done or is reformulating the search string the only way in such a case?
Maybe you could try to use Conditional Insertions mentioned in the TM manual chapter 20.4.3 http://manual.macromates.com/en/regular_expressions
Naïve example:
I have the string
123456
and I want to get
123457
by using this regexp:
123(.)(.)7
this won't work:
123$1$27 because $27 isn't set
but this should work
123$1$2(?0:)7
(?0:) is a kind of dummy and inserts nothing
Cheers,
--Hans
2008/9/2 Hans-Jörg Bibiko bibiko@eva.mpg.de
Maybe you could try to use Conditional Insertions mentioned in the TM manual chapter 20.4.3 [...] (?0:) is a kind of dummy and inserts nothing
Good idea, thanks! I didn't know that ...
Chris
Hi
That is a very useful thing to know about. It does strike me as very strange that the Oniguruma Regular Expressions system is like this.
I haven't as yet encountered a situation where this was going to be a problem but I'd noticed that it doesn't seem to be accounted for in Oniguruma.
In the system used by BBEedit¹ the matching expression is two or three characters long, where the last one or two characters or digits.
So, \1 matches the first group in the find expression and \01 does the same. If you want to replace this followed by the digit "2" then you put \012 in the replace expression. This is no problem up until 99 matches of course :)
¹Apologies for mentioning BBEdit :)
Patrick
On 2 Sep 2008, at 16:53, Hans-Jörg Bibiko wrote:
Maybe you could try to use Conditional Insertions mentioned in the TM manual chapter 20.4.3 http://manual.macromates.com/en/regular_expressions
Naïve example:
I have the string
123456
and I want to get
123457
by using this regexp:
123(.)(.)7
this won't work:
123$1$27 because $27 isn't set
but this should work
123$1$2(?0:)7
(?0:) is a kind of dummy and inserts nothing
On 2 Sep 2008, at 21:40, Patrick James wrote:
That is a very useful thing to know about. It does strike me as very strange that the Oniguruma Regular Expressions system is like this.
For the records, the replacement format string is completely unrelated to Oniguruma.
I haven't as yet encountered a situation where this was going to be a problem but I'd noticed that it doesn't seem to be accounted for in Oniguruma.
It is not, no, except for using conditional insertions as sort of a hack. There will however be a significant update to this syntax in… the future :) This will allow using ${1} etc. like in snippets.
On 03.09.2008, at 12:25, Allan Odgaard wrote:
On 2 Sep 2008, at 21:40, Patrick James wrote:
That is a very useful thing to know about. It does strike me as very strange that the Oniguruma Regular Expressions system is like this.
For the records, the replacement format string is completely unrelated to Oniguruma.
Is there also a plan to support within the replacement format string Oniguruma's named groups and the entire back reference functionality (including back reference with nested levels)?
Cheers,
--Hans
On 3 Sep 2008, at 12:53, Hans-Jörg Bibiko wrote:
On 03.09.2008, at 12:25, Allan Odgaard wrote:
On 2 Sep 2008, at 21:40, Patrick James wrote:
That is a very useful thing to know about. It does strike me as very strange that the Oniguruma Regular Expressions system is like this.
For the records, the replacement format string is completely unrelated to Oniguruma.
Is there also a plan to support within the replacement format string Oniguruma's named groups and the entire back reference functionality (including back reference with nested levels)?
The named captures will be available as $variables. Not sure what the other thing you refer to is. Can you give an example?
On 03.09.2008, at 18:17, Allan Odgaard wrote:
On 3 Sep 2008, at 12:53, Hans-Jörg Bibiko wrote:
Is there also a plan to support within the replacement format string Oniguruma's named groups and the entire back reference functionality (including back reference with nested levels)?
The named captures will be available as $variables.
This will be awesome ;)
Not sure what the other thing you refer to is. Can you give an example?
OK. Maybe you remember Thomas Aylott and I fiddled around to implement a command which is able to select/find balanced HTML/XML tags. Finally we found a solution but it makes usage of many many lines of source code (the command should be in the TM trunk experimental).
Some while ago I read Oniguruma's RE.txt carefully. This kind of match is supported natively by Oniguruma. It is called 'back reference with nest level'.
Example 1:
I have a string: "<foo>f<foo>b<bar>123<bar>456</bar></bar>bb</foo>f</ foo>" and this regexp (please don't be frightened ;):
(?<element>\g<stag>\g<content>*\g<etag>){0}(?<stag><\g<name>\s*>){0}(? <name>[a-zA-Z_:]+){0}(?<content>[^<&]+(\g<element>|[^<&]+)*){0}(? <etag></\k<name+1>>){0}\g<element>
If I run this through Oniguruma I get these named groups: [syntax: group-name (which group): (string-indices[start-stop]]) content]
stag (2): (20-25) <bar> content (4): (5-49) f<foo>b<bar>123<bar>456</bar></bar>bb</foo>f element (1): (0-55) <foo>f<foo>b<bar>123<bar>456</bar></bar>bb</ foo>f</foo> etag (5): (49-55) </foo> name (3): (21-24) bar
Example 2: string: "o>b<bar>123<bar>456</bar></bar>bb</foo>f</foo>"
stag (2): (11-16) <bar> content (4): (8-25) 123<bar>456</bar> element (1): (3-31) <bar>123<bar>456</bar></bar> etag (5): (25-31) </bar> name (3): (12-15) <bar>
In other words it should be possible to use Oniguruma's power to find/ select the next balanced HTML/XML tag by using only one more or less easy regular expression depending of the position of the caret.
As far as I know Ruby 1.9 is supporting this (?). By myself I'm using a C program linked to the onig lib to match these nested stuff.
Furthermore this issue leads to a question: Would it be possible to use TM's Oniguruma engine from outside, meaning an API?
Best,
--Hans
OK. Maybe you remember Thomas Aylott and I fiddled around to implement a command which is able to select/find balanced HTML/XML tags. Finally we found a solution but it makes usage of many many lines of source code (the command should be in the TM trunk experimental).
Some while ago I read Oniguruma's RE.txt carefully. This kind of match is supported natively by Oniguruma. It is called 'back reference with nest level'.
Example 1:
I have a string: "<foo>f<foo>b<bar>123<bar>456</bar></bar>bb</foo>f</ foo>" and this regexp (please don't be frightened ;):
(?<element>\g<stag>\g<content>*\g<etag>){0}(?<stag><\g<name>\s*>){0}(? <name>[a-zA-Z_:]+){0}(?<content>[^<&]+(\g<element>|[^<&]+)*){0}(? <etag></\k<name+1>>){0}\g<element>
Stop. I've just tried out the regexp with TM and IT ALREADY WORKS ;-O [I could swear that I tried it out some while ago]
I believe I did an error in reasoning. This has nothing to do with the replacement format string. All these nested stuff is only relevant within the regexp.
--Hans
On Sep 4, 2008, at 3:30 AM, Hans-Jörg Bibiko wrote:
Stop. I've just tried out the regexp with TM and IT ALREADY WORKS ;-O
OK. You have my attention. Does this mean we'll be able to get the holy grail of full html balanced tag selection with just a regular expression?
—Thomas Aylott / subtleGradient
On 08.09.2008, at 00:26, Thomas Aylott wrote:
On Sep 4, 2008, at 3:30 AM, Hans-Jörg Bibiko wrote:
Stop. I've just tried out the regexp with TM and IT ALREADY WORKS ;-O
OK. You have my attention. Does this mean we'll be able to get the holy grail of full html balanced tag selection with just a regular expression?
Well, in principal yes, but one has to try to fine-tune the regexp. The mentioned regexp does match all tags like <a>..</a> but not <img> or <img />. If you have such a tag in the doc the regexp engine runs in a loop. I do not know if we need a tiny script around the regexp but at least it would simplify the script for matching balanced tags. This week I'll have no time.
--Hans
On 3 Sep 2008, at 22:04, Hans-Jörg Bibiko wrote:
[...] Furthermore this issue leads to a question: Would it be possible to use TM's Oniguruma engine from outside, meaning an API?
API accessible from where? I.e. ‘outside’ would likely be another programming language, and then you probably already have a regexp library for that language.