0

I would like to match, using regex with groups, in java these scenarios but I am not very good with regex and don't understand how to match repeating things like these:

Text: local var1, var2, var3 = 100

I want to match to get all the lua variable names so my matches would be 'var1' and 'var2' and 'var3'

Text: self.var1, self.var2 = 200

Same as above but using self instead of local- I'd like matches of 'var1' and 'var2'

And lastly Text: var1, var2, var3 = 300

I imagine I may be able to use the first regex for this, right? I'd like the matches to work for 1 or more matches so these all would work:

var1 = 10

var1, var2 = 10

self.v = 1

self.v1, self.v2 = 20

local v1,v2 = 10

local v1 = 30

I do not need all this in one big regex, 2 or 3 different patterns would work. (Hope this type of question is appropriate since I don't really have any code to show...)

Thanks in advance!

1 Answer 1

2

I think something like this should work

String input = "...Your input...";
List<String> matches = new ArrayList<>();

Matcher m = Pattern.compile("(\\w+)\\s*(?=[,=])").matcher(input);
while(m.find()) {
    matches.add(m.group(1));
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, works well with a little tweaking for my purpose!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.