Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

get recursion number or use result of group in later expression?

Last post 10-16-2012, 1:11 AM by Aussie Susan. 1 replies.
Sort Posts: Previous Next
  •  10-15-2012, 6:01 AM 86847

    get recursion number or use result of group in later expression?


    I need to match consecutive numbers, and repeat the same piece of regex as long as they keep incrementing, and what would really reduce my expression, 250+ characters long already! (or rather make it more elegant) would be to:

    be able to use the result from one capture group in another expression later


    to get the current iteration number and match other expressions against

    PS: It HAS to be regex, not other programming involved.


    text 1 matches

    text 2 matches

    text 3 matches

    text 6 doesn't match. end.

    text 1 shouldn't even get here (or start over?)

    - I know I can do it with nested conditionals, but I don't want to manually add one expression for each condition.


  •  10-16-2012, 1:11 AM 86851 in reply to 86847

    Re: get recursion number or use result of group in later expression?

    There are several issues at play here. The first is that there is no such thing as a single "regex engine" and each has different capabilities and, in many cases, syntax extensions to let you access those capabilities.Therefore you will need to tell us the regex variant you are using.

    However, there is a general rule with regex patterns that they work on characters and have absolutely no idea what those characters "mean". This is especially true when you use words like "numbers" and "letters" - what you are normally actually using are character sets that just happen to be made up of the characters that we use as digits or as letters. Further, a regex has no concept of numbers so "consecutive" is meaningless to the regex itself.

    What you might be able to do is use a regex that recognises consecutive digits as a match and then calls a "delegate" or "callback" function that will receive the matched characters, convert them to a number, compare against any previous match and signal back to the regex if the match is acceptable. Not many regex engines allow the calling of user-written functions, most that do have then in the "replace" situation and none that I know of will signal back to the regex engine that a match should be rejected. However such a beast may exist.

    What you may need to do is to to write a pattern that locates all of the "numbers" and then returns all of those in an array (most regex libraries will let you do that). The returned items are normally a structure that includes the starting offset and either the end offset or the length of the matched characters. You can then perform whatever processing you need to on the array of matches.

    You certainly can use the captured characters from one match group later on, but there are no operators available within the regex pattern language that would let you do anything with those captured characters except use them directly. (I.e. '(\w+).*?\1' would match from whatever matched the '\w+' to wherever those same letters occurred again - given the text "expressions are pressing" you get 2 matches of ""expre" (the "e" to the next "e") and "ssions are pressi" (the "ssi" to the next "ssi").


View as RSS news feed in XML