python - Split string by number of whitespaces -


i have string looks either of these 3 examples:

1: name             = astring               comments 2: typ              = 1 2 thee             must "sand", "mud" or "bedload" 3: rdw    = 0.02      [ - ] comment rdw 

i first split variable name , rest so:

re.findall(r'\s*([a-za-z0-9_]+)\s*=\s*(.*)', line) 

i want split right part of string part containing values , part containing comments (if there any). want looking @ number of whitespaces. if exceeds 4, assume comments start

any idea on how this?

i have

re.findall(r'(?:(\s+)\s{0,3})+', datastring) 

however if test using string:

'aa    aa23r234rf2134213^$&$%&            bb' 

then selects 'bb'

you may use single regex re.findall:

^\s*(\w+)\s*=\s*(.*?)(?:(?:\s{4,}|\[)(.*))?$ 

see regex demo.

details:

  • ^ - start of string
  • \s* - 0+ whitespaces
  • (\w+) - capturing group #1 matching 1 or more letters/digits/underscores
  • \s*=\s* - = enclosed 0+ whitespaces
  • (.*?) - capturing group #2 matching 0+ chars, few possible, first...
  • (?:(?:\s{4,}|\[)(.*))? - optional group matching
    • (?:\s{4,}|\[) - 4 or more whitespaces or [
    • (.*) - capturing group #3 matching 0+ chars to
  • $ - end of string.

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -