简体   繁体   中英

Regex to find a substring using regex

I'm using regex with Groovy(Grails) to find a substring which is a combination of capitalized alphabets, underscores and digits only.

The regex

"THIS_WORD" ==~ /([A-Z_0-9]*)/

returns true ( but the following statement

def str = "Wlkjjf alkjdfas Wk;ljdfs fk THIS_WORD dsklafjf kjd".findAll{([A-Z_0-9]*)/}
println str

returns [W, W, T, H, I, S, _, W, O, R, D]

I need only the word THIS_WORD not alphabet W that is repeated twice. What am I missing here?

也许您可以使用{2,}而不是*来获取所有具有1个以上字符的匹配项:

def str = "Wlkjjf als Wk;lfs fk THIS_WORD dsjf kjd".findAll(/[A-Z_0-9]{2,}/)
  • means 0 or more whereas a + means 1 or more. To do 2 or more you would need to use the {MIN,MAX} syntax after the []

([A-Z0-9_]{2,})

After learning a bit about groovy and testing on the groovy console at http://groovyconsole.appspot.com/ I found this worked.

def str = "Wlkjjf alkjdfas Wk;ljdfs fk THIS_WORD dsklafjf kjd".findAll(/([A-Z_0-9]{2,})/)
println str​​​​​​​​​​​​

def str = "Wlkjjf alkjdfas Wk;ljdfs fk THIS_WORD dsklafjf kjd".findAll{([A-Z_0-9]*)/}

This doesn't compile. Perhaps you meant this:

"Wlkjjf alkjdfas Wk;ljdfs fk THIS_WORD dsklafjf kjd".findAll(/[A-Z_0-9]*/)

which gives

[W, , , , , , , , , , , , , , , , W, , , , , , , , , , , , THIS_WORD, , , , , , , , , , , , , , ]

If you are looking for all upper-case words, a regex like this will work better:

"Wlkjjf alkjdfas Wk;ljdfs fk THIS_WORD dsklafjf kjd".findAll(/\b[A-Z_0-9]+\b/)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM