I’m trying to get all the names in a string like this:
:name, :lastName
But I don’t seem to find a correct way.
This is what I’ve tried so far:
/^(:((\w+)(,:(\w+))+).*)$/
In Java:
Pattern a = Pattern.compile("(:((\\w+)(,:(\\w+))+).*)");
Matcher m = a.matcher(":name,:lastName,:bd");
if( m.matches() ) {
for( int i = 0 ; i < m.groupCount() ; i++ ) {
out.println( i + " = " + m.group( i ) );
}
}
Output:
0 = :name,:lastName,:bd
1 = :name,:lastName,:bd
2 = name,:lastName,:bd
3 = name
4 = ,:bd
And I’m trying to get a variable number of groups containing [name, lastName, bd]
EDIT
BTW, I’m trying to get this for a more complex regex to match simple things like:
insert into table values ( :a, :b, :c )
/insert\s+into\s+(\w+)\s+values\s+(\( HERE IS MY QUESTION \))/
Is it a requirement that you place the result in different groups? This will oterwise work:
Edit: … and you can use split if you want to get an array of results: