java - Regular expression with repetitive pattern -
i need regular expression match
%foo(%bar) or %foo.
i'm using code now:
string code = "%foo(%bar)"; code.matches("%+[\w&&[^_]][\w0-9]+[\w0-9&&[^_]]");
but works in second case.
edit: regular expression must check if variable contain numbers , letters , if start _ or end _. don't want match _%foo
or %foo_
or _%foo_
.
a correct java regular expression be
pattern pat = pattern.compile("%\\w+(?:\\(%\\w+\\))?");
no underscores in first or last place:
pattern pat = pattern.compile("%[\\w&&[^_]]\\w*[\\w&&[^_]]" + "(?:\\(%[\\w&&[^_]]\\w*[\\w&&[^_]]\\))?");
this pattern requires @ least 2 characters in name. if single non-underscore should accepted, second , third part can made optional:
pattern pat = pattern.compile("%[\\w&&[^_]](?:\\w*[\\w&&[^_]])?" + "(?:\\(%[\\w&&[^_]](?:\\w*[\\w&&[^_]])?\\))?");
Comments
Post a Comment