splitting string using regular expression in python
<p>Hi guys I want to split the following string that are parsed from a text file using perhaps regular expression in python.</p>

<pre><code>Inside the text file(filename.txt)

iPhone.Case.1.left=1099.0.2
new.phone.newwork=bla.jpg
</code></pre>

<p>I want a function that when looping through arrayOfStrings wii split it so that the following is displayed</p>

<pre><code>['iPhone','Case','1','left','1099.0.2']

['new','phone','newwork','bla.jpg'] 
</code></pre>

<p>This is what I have done so far</p>

<pre><code>import re
pattern = '(?&lt;!\d)[\.=]|[\.=](?!\d)'

f = open('filename.txt','rb')
for line in data_file:
   str_values = re.split(pattern, line.rstrip())
   print str_values
</code></pre>

<p>This is what is being printed</p>

<pre><code>['iPhone', 'Case', '1', 'left', '1099.0.2']
['new', 'phone', 'newwork', 'bla', 'jpg']
</code></pre>

<p>but I want the last array to be </p>

<pre><code>['new','phone','newwork','bla.jpg']
</code></pre>

<p>If you have regular enough input data that you can always split first at the <code>=</code> character, then split the first half at every <code>.</code> character, I would skip regex entirely, as it is complicated and not very pretty to read.</p>

<p>Here's an example of doing just that:</p>

<pre><code>s = 'new.phone.newwork=bla.jpg'
l = str.split(s.split('=')[0], '.') + s.split('=')[1:]
</code></pre>

python regex python-2.7