I’m trying to parse through a string formatted like this, except with more values:
Key1=value,Key2=value,Key3=value,Key4=value,Key5=value,Key6=value,Key7=value
The Regex
((Key1)=(.*)),((Key2)=(.*)),((Key3)=(.*)),((Key4)=(.*)),((Key5)=(.*)),((Key6)=(.*)),((Key7)=(.*))
In the actual string, there are about double the amount of key/values, but I’m keeping it short for brevity. I have them in parentheses so I can call them in groups. The keys I have stored as Constants, and they will always be the same. The problem is, it never finds a match which doesn’t make sense (unless the Regex is wrong)
Judging by your comment above, it sounds like you’re creating the Pattern and Matcher objects and associating the Matcher with the target string, but you aren’t actually applying the regex. That’s a very common mistake. Here’s the full sequence:
Not only do you have to call
find()ormatches()(orlookingAt(), but nobody ever uses that one), you should always call it in aniforwhilestatement–that is, you should make sure the regex actually worked before you call any methods likegroup()that require the Matcher to be in a “matched” state.Also notice the absence of most of your parentheses. They weren’t necessary, and leaving them out makes it easier to (1) read the regex and (2) keep track of the group numbers.