scala - Match words which have given regex around -


i need text have 2 or more spaces "\s{2,}" around them.

given following text:

ip address  name           location          type 10.1.10.5   uslaxbowc01rb  santa monica, ca  local 


need extract:

line1: "ip address", "name", "location", "type"
line2: "10.1.10.5", "uslaxbowc01rb", "santa monica, ca", "local"

edit:

text eligible extraction:

"ip address" & "name" 2 or more spaces apart eligible extracted. similarly, "santa monica, ca" & "local".

you try split text according pattern "\s{2,}".

thus, in python, regex lib re give needed tools:

import re line   = "ip address  name           location          type"  result = re.split('\s{2,}',line) 

which gives:

['ip address', 'name', 'location', 'type'] 

edit

i guess understood little more question : more care isolating sequence between \s{2,}, splitting it. in example, however, solution above seems suitable.

you asked regex, here :

reg1 = "[^\s](?!\s{2,})(?:.(?!\s{2,}))*[^\s]" 
  1. it first selects character not space [^\s](?!\s{2,}) not followed 2 spaces or more. so, used negative lookahead assertion (?!...) ;
  2. then, isolates group (?:...) composed in way : character . not followed \s{2,} ;
  3. repeat * ;
  4. it happens final character not selected if stop now. should add 1 more [^\s].

a re.findall(reg1,line), , should done. 1 drawback maybe : it detects sequences @ least 2 characters long.

in case, other , simpler regex complete job : reg2 = "\s{2,}([^\s])\s{2,}". selects single non-space characters surrounded 2 spaces or more. use of bracket (...), forces return character.

by way, advise on documentation : https://docs.python.org/2/library/re.html

hope found looking :-)


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -