开发者

Enforce sequence and group same time with regex?

开发者 https://www.devze.com 2023-03-14 09:21 出处:网络
A bit of continuation of Get groups with regex and OR Sample AD ABCDEFG HIJKLMN AB HIJKLMN AC DJKEJKW SJKLAJL JSHELSJ

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})+)/

I can access the 2chars code with group[0] and so on.

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

  1. I'd check the string matches a regular expression
  2. 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]]
0

精彩评论

暂无评论...
验证码 换一张
取 消