Hi,
I'm trying to write a helper method that breaks down path expressions and would love some help. Please consider a path pattern like the following four (round brackets indicate predicates):
item.sub_element.subsubelement(@key = string)
; or,item..subsub_element(@key = string)
; or,//subsub_element(@key = string)
; or,item(@key = string)
what would a regular expression look like that matches those?
What I have come up with is this:
((/{2}?[\\w+_*])(\\([_=@#\\w+\\*\\(\\)\\{\\}\\[\\]]*\\))?\\.{0,2})+
I'm reading this as: "match one or more occurences of a string that consists of two groups: group one consists of one or more words with optional underscores and optional double forward slash prefix ; group two is optional and consists of at least one word with all other characters optional ; groups are trailed by zero to two dots."
However, a test run on the fourth example with Matcher.matches() returns false. So, where's my error?
Any ideas?
TIA,
FK
Edit: from trying with http://www.regexplanet.com/simple/index.html it seems I wasn't aware of the difference between the Matcher.matches()
and the Matcher.find()
methods of the Matcher
object. I was trying to break down the input string in to substrings that match my regex. Consequently I need to use find()
, not matches()
.
Edit2: This does the trick
([a-zA-Z0-9_]+)\.{0,2}(\(.*\))?