1

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

Inside the text file(filename.txt)

iPhone.Case.1.left=1099.0.2
new.phone.newwork=bla.jpg

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

['iPhone','Case','1','left','1099.0.2']

['new','phone','newwork','bla.jpg'] 

This is what I have done so far

import re
pattern = '(?<!\d)[\.=]|[\.=](?!\d)'

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

This is what is being printed

['iPhone', 'Case', '1', 'left', '1099.0.2']
['new', 'phone', 'newwork', 'bla', 'jpg']

but I want the last array to be

['new','phone','newwork','bla.jpg']
4
  • Yup, that's certainly doable. Have you tried anything? We're not going to write your code for you. Commented Sep 6, 2016 at 19:44
  • @MorganThrapp Yes I have let me edit the question Commented Sep 6, 2016 at 19:45
  • 1
    What is the rule for deciding whether a given dot is a character on which to split? I see that you want to split on the dot between "iPhone" and "Case" but not on the dot between "1099" and "0". Also, are dot and equals the only characters on which you could possibly split? Finally, you say that you want something "displayed," but I imagine you really want a data structure -- a list of lists, perhaps, where each internal list is the split-up version of an entry in the input list? Commented Sep 6, 2016 at 19:47
  • @D-Von the idea is that it should not split when a digit comes before and after a dot and it should not split with image extensions Commented Sep 6, 2016 at 19:59

2 Answers 2

1

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

Here's an example of doing just that:

s = 'new.phone.newwork=bla.jpg'
l = str.split(s.split('=')[0], '.') + s.split('=')[1:]
Sign up to request clarification or add additional context in comments.

Comments

0

Try this :

% python
>>> import re
>>> arrayOfStrings =["iPhone.Case.1.left=1099.0.2", " new.phone.newwork=bla.jpg"]
>>> def printStuff(arg):
...     for i,x in enumerate(arg):
>>>         print(arg[i].split('=')[i].split('.') + [ arg[i].split('=')[1] ])
...
>>> printStuff(arrayOfStrings)
['iPhone', 'Case', '1', 'left', '1099.0.2']
['bla', 'jpg', 'bla.jpg']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.