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

Popular posts from this blog

How to show in django cms breadcrumbs full path? -

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -