I am trying to use regular expressions to match some text.
The following pattern is what I am trying to gather.
@Identifier('VariableA', 'VariableB', 'VariableX', ..., 'VariableZ')
I would like to grab a dynamic number of variables rather than a fixed set of two or three.
Is there any way to do this? I have an existing Regular Expression:
\@(\w+)\W+(\w+)\W+(\w+)\W+(\w+)
This captures the Identifier and up to three variables.
Edit: Is it just me, or are regular expressions not as powerful as I’m making them out to be?
You want to use
scanfor this sort of thing. The basic pattern would be this:That would give you an array of all the contiguous sequences for word characters:
You say you might have multiple instances of your pattern with arbitrary stuff surrounding them. You can deal with that with nested
scans:That will give you an array of arrays, each inner array will have the “Identifier” part as the first element and that “Variable” parts as an array in the second element. For example:
If you might be facing escaped quotes inside your “Variable” bits then you’ll need something more complex.
Some notes on the expression:
You don’t really need
[^)]+?here, just[^)]+would do but I use the non-greedy forms by habit because that’s usually what I mean. The grouping is used to separate the@IdentifierandVariableparts so that we can easily get the desired nested array output.