Friday, 15 April 2011

java - ReGex patten to match conditional variable names? -



java - ReGex patten to match <c:if > conditional variable names? -

i need conditional variable name cases in particular jsp reading jsp line line , searching particular pattern line checking 2 type of cond finds match

<c:if condition="event ='confirmation'"> <c:if condition="event1 = 'confirmation' or event2 = 'action'or event3 = 'check'" .....>

desired result name of cond variable - event,event1,event2,event3 have written parser satisfying first case not able find variable names sec case.need pattern satisfy both of them.

string stringsearch = "<c:if"; while ((line = bf.readline()) != null) { // increment count , find index of word linecount++; int indexfound = line.indexof(stringsearch); if (indexfound > -1) { pattern pattern = pattern .compile(test=\"([\\!\\(]*)(.*?)([\\=\\)\\s\\.\\>\\[\\(]+?)); matcher matcher = pattern.matcher(line); if (matcher.find()) { str = matcher.group(1); hset.add(str); counter++; } }

if understood requirement well, may work :

("|\s+)!?(\w+?)\s*=\s*'.*?'

$2 give each status variable name.

what is:

("|\s+) " or one or more spaces

!? optional !

(\w+?) 1 or more word character (letter, digit or underscore) (([a-za-z]\w*) more correct)

\s*=\s* = preceded , followed 0 or more spaces

'.*?' 0 or more characters within ' , '

second capture grouping (\w+?) retrieving variable name

add required escaping \

edit: additional conditions specified, next may suffice:

("|or\s+|and\s+)!?(\w+?)(\[\d+\]|\..*?)?\s*(!?=|>=?|<=?)\s*.*?

("|or\s+|and\s+) " or or followed 1 or more spaces or and followed 1 or more spaces. (here, assumed each look part or variable name preceded " or or followed 1 or more spaces or and followed 1 or more spaces)

!?(\w+?) optional ! followed 1 or more word character

(\[\d+\]|\..*?)? optional part constituting a number enclosed in square brackets or a dot followed 0 or more characters

(!?=|>=?|<=?) of next relational operators : =,!=,>,<,>=,<=

$2 give variable name.

here sec capture grouping (\w+?) retrieving variable name , 3rd capture grouping retrieves suffix if nowadays (eg:[2] in event[2]).

for input containing status event.indexof(2)=something, $2 gives event only. if want event.indexof(2) utilize $2$3.

java regex jsp

No comments:

Post a Comment