Parsing Text with Python RegEx re.findall
Question: I have a long string that I need to parse in groups, but need to control it
more.

    
    
    import re
    
    RAW_Data = "Name Multiple Words Testing With 1234 Numbers and this stuff* ((Bla Bla Bla (Bla Bla) A40 & A41)) Name Multiple Words Testing With 3456 Numbers and this stuff2* ((Bla Bla Bla (Bla Bla) A42 & A43)) Name Multiple Words Testing With 78910 Numbers and this stuff3* ((Bla Bla Bla (Bla Bla) A44 & A45)) Name Multiple Words Testing With 1234 Numbers and this stuff4* ((Bla Bla Bla (Bla Bla) A46 & A47)) Name Multiple Words Testing With 1234 Numbers and this stuff5* ((Bla Bla Bla (Bla Bla) A48 & A49)) Name Multiple Words Testing With 1234 Numbers and this stuff6* ((Bla Bla Bla (Bla Bla) A50 & A51)) Name Multiple Words Testing With 1234 Numbers and this stuff7* ((Bla Bla Bla (Bla Bla) A52 & A53)) Name Multiple Words Testing With 1234 Numbers and this stuff8* ((Bla Bla Bla (Bla Bla) A54 & A55)) Name Multiple Words Testing With 1234 Numbers and this stuff9* ((Bla Bla Bla (Bla Bla) A56 & A57)) Name Multiple Words Testing With 1234 Numbers and this stuff10* ((Bla Bla Bla (Bla Bla) A58 & A59)) Name Multiple Words Testing With 1234 Numbers and this stuff11* ((Bla Bla Bla (Bla Bla) A60 & A61)) Name Multiple Words Testing With 1234 Numbers and this stuff12* ((Bla Bla Bla (Bla Bla) A62 & A63)) Name Multiple Words Testing With 1234 Numbers and this stuff13* ((Bla Bla Bla (Bla Bla) A64 & A65)) Name Multiple Words Testing With 1234 Numbers and this stuff14* ((Bla Bla Bla (Bla Bla) A66 & A67)) Name Multiple Words Testing With 1234 Numbers and this stuff15* ((Bla Bla Bla (Bla Bla) A68 & A69)) Name Multiple Words Testing With 1234 Numbers and this stuff16*"
    
    fromnode = re.findall('(.*?)(?=\*\s)', RAW_Data)
    
    print fromnode
    
    del fromnode
    del RAW_Data
    

The results are: 'Name Multiple Words Testing With 1234 Numbers and this
stuff', '', ' ((Bla Bla Bla (Bla Bla) A40 & A41)) Name Multiple Words Testing
With 3456 Numbers and this stuff2' _........ and so on._

I can't seem to capture only the strings like "Name Multiple Words Testing
With 3456 Numbers and this stuff" and omit all of the strings like "((Bla Bla
Bla (Bla Bla) A40 & A41))". Any help would be much appreciated.


Answer: You can split with

    
    
    r'\*\s*\({2}.*?\){2}\s*'
    

The pattern ([see demo](https://regex101.com/r/yH8eJ0/1)) matches:

  * `\*` \- a literal asterisk
  * `\s*` \- zero or more whitespaces
  * `\({2}` \- exactly 2 opening parentheses
  * `.*?` \- zero or more characters other than a newline (NOTE: add the `re.S` flag if you need to match across several lines) as few as possible up to the first
  * `\){2}` \- double closing parentheses
  * `\s*` \- 0+ whitespace.

ALSO: The [same, but unrolled (thus, a bit more efficient)
regex](https://regex101.com/r/yH8eJ0/2):

    
    
    \*\s*\({2}[^)]*(?:\)(?!\))[^)]*)*\){2}\s*
    

See [IDEONE demo](http://ideone.com/XjrxIw):

    
    
    import re
    p = re.compile(r'\*\s*\({2}.*?\){2}\s*')
    test_str = "Name Multiple Words Testing With 1234 Numbers and this stuff* ((Bla Bla Bla (Bla Bla) A40 & A41)) Name Multiple Words Testing With 3456 Numbers and this stuff2* ((Bla Bla Bla (Bla Bla) A42 & A43)) Name Multiple Words Testing With 78910 Numbers and this stuff3* ((Bla Bla Bla (Bla Bla) A44 & A45)) Name Multiple Words Testing With 1234 Numbers and this stuff4* ((Bla Bla Bla (Bla Bla) A46 & A47)) Name Multiple Words Testing With 1234 Numbers and this stuff5* ((Bla Bla Bla (Bla Bla) A48 & A49)) Name Multiple Words Testing With 1234 Numbers and this stuff6* ((Bla Bla Bla (Bla Bla) A50 & A51)) Name Multiple Words Testing With 1234 Numbers and this stuff7* ((Bla Bla Bla (Bla Bla) A52 & A53)) Name Multiple Words Testing With 1234 Numbers and this stuff8* ((Bla Bla Bla (Bla Bla) A54 & A55)) Name Multiple Words Testing With 1234 Numbers and this stuff9* ((Bla Bla Bla (Bla Bla) A56 & A57)) Name Multiple Words Testing With 1234 Numbers and this stuff10* ((Bla Bla Bla (Bla Bla) A58 & A59)) Name Multiple Words Testing With 1234 Numbers and this stuff11* ((Bla Bla Bla (Bla Bla) A60 & A61)) Name Multiple Words Testing With 1234 Numbers and this stuff12* ((Bla Bla Bla (Bla Bla) A62 & A63)) Name Multiple Words Testing With 1234 Numbers and this stuff13* ((Bla Bla Bla (Bla Bla) A64 & A65)) Name Multiple Words Testing With 1234 Numbers and this stuff14* ((Bla Bla Bla (Bla Bla) A66 & A67)) Name Multiple Words Testing With 1234 Numbers and this stuff15* ((Bla Bla Bla (Bla Bla) A68 & A69)) Name Multiple Words Testing With 1234 Numbers and this stuff16*"
    print(re.split(p, test_str))
    

**UPDATE**

A regex for use with `re.findall`:

    
    
    (?:\*\s*\(\([^)]*(?:\)(?!\))[^)]*)*\)\))?\s*([^*]*(?:\*(?!\s*\(\()[^*]*)*)\s*
    

See the [regex demo](https://regex101.com/r/bG6tM6/1)

Horrified at the looks of it? It is just the unrolled version of a much
simpler
[`(?:\*\s*\(\(.*?\)\))?\s*(.*?(?=\*\s*(?:\(\(|$)))`](https://regex101.com/r/oX9fF9/1).

See the [IDEONE demo](http://ideone.com/WoiFJn).

