Enforce sequence and group same time with regex?
A bit of continuation of Get groups with regex and OR
Sample
AD ABCDEFG HIJKLMN
AB HIJKLMN AC DJKEJKW SJKLAJL JSHELSJ
Rule: Always 2 Chars Code (AB|AC|AD) at line beginning then any number (>1) of 7 Chars codes following (at least one 7char code). The space between the groups also can be a '.'
With this expression I get it nicely grouped
/^(AB|AC|AD)|((\S{7})+)/
Can I enforce the rule开发者_运维百科 as above the same time ?
With above regex the following lines are also valid (because of the OR | in the regex statement)
AC
dfghjkl asdfgh hjklpoi
Which is not what I need.
Thanks again to the regex experts
Try that:
^(A[BCD])(([ .])([A-Z]{7}))+$
Personally, I would do this in two separate steps
- I'd check the string matches a regular expression
- I'd
split
matching strings based on the separator chars[ .]
This code:
def input = [
'AD ABCDEFG HIJKLMN',
'AB HIJKLMN',
'AC DJKEJKW SJKLAJL JSHELSJ',
'AC',
'dfghjkl',
'asdfgh hjklpoi',
'AC DJKEJKW.SJKLAJL JSHELSJ',
]
def regexp = /^A[BCD]([ .](\S{7}))+$/
def result = input.inject( [] ) { list, inp ->
// Does the line match the regexp?
if( inp ==~ regexp ) {
// If so, split it
list << inp.split( /[ .]/ )
}
list
}
println result
Shows you an example of what I mean, and prints out:
[[AD, ABCDEFG, HIJKLMN], [AB, HIJKLMN], [AC, DJKEJKW, SJKLAJL, JSHELSJ], [AC, DJKEJKW, SJKLAJL, JSHELSJ]]
精彩评论