6

Expression corresponding to each parameter in the delimited block

 3 years ago
source link: https://www.codesd.com/item/expression-corresponding-to-each-parameter-in-the-delimited-block.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Expression corresponding to each parameter in the delimited block

advertisements

In esoteric scripting language I have structures like those:

node % % % % % ... end

The '%' character represent an identifier, but let's leave it for now. All I want to do here is match every '%' and nothing more.

In text term: let's match every percent which is between 'node' and 'end' delimiters.

My guess was:

(?<=node )(?:(?<= )%)+(?= end)

But no, it only matches single '%' in

node % end

Any clues?

NOTE: The language is C#. So "\K" won't work. Important condition - I need pure matches, I can't use capture groups. It's a limitation of the parser, but if it can't be done without capture groups, I'll be forced to override built in parser and write my own, but I still hope it could be avoided somehow.

I need this expression to display hints for parameter numbers in editor. When you place mouse cursor over third expression in "node ... end" editor would show hint "P3". The real code would replace % with regex matching identifier "[^ ;\r\n]+", and the space with "[ ;\r\n]+". If it cannot be done in C# without using capture groups I'll just have to add a few lines of code to select ranges for hints manually. But it would be not as cool as single regex :)


This is not going to have the best performance ever I guess, but unless you can provide better constrains you will have to use 2 .* wildcards (make sure that . does not match new lines):

(?<=node.*)%(?=.*end)


Edit: according to http://www.regular-expressions.info/lookaround.html#limitbehind, .NET supports infinite repetition lookbehinds, even if the above regex is not a valid PCRE (it would cause the error "Lookbehinds need to be zero-width, thus quantifiers are not allowed" in other languages).

Tags regex

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK