-1

There is an string like this

100Usable by Everybody: Design Principles for Accessibility on Mac OS X 101What's New in Cocoa Touch 102What's New in Foundation for iOS 4 103iPad and iPhone User Interface Design 104Designing Apps with Scroll Views 308Developing Your App with Xcode 4 309Advanced Performance Analysis with Instruments 419OpenGL ES Tuning and Optimization 420OpenGL for Mac OS X 421Incorporating the Camera and Photo Library in your App 422Taking Advantage of Multiple GPUs

I want to find all number substring, so I get an set of number string,one of them will be like ‘100’. Then I modify it, append '\n- ' before it. So finally I will get an string like this

- 100 Usable by Everybody: Design Principles for Accessibility on Mac OS X 
- 101 What's New in Cocoa Touch 
- 102 What's New in Foundation for iOS 4 
- 103 iPad and iPhone User Interface Design 
- 104 Designing Apps with Scroll Views
- 308 Developing Your App with Xcode 4 
- 309 Advanced Performance Analysis with Instruments 
- 419 OpenGL ES Tuning and Optimization 
- 420 OpenGL for Mac OS X 
- 421 Incorporating the Camera and Photo Library in your App 
- 422 Taking Advantage of Multiple GPUs

How can i do this with python, please help. Thanks very much.


After find the number substring, I use it. For example, I find '100', then it will be replace with '\n - 100 '.

4
  • 2
    Possible duplicate of python string search replace Commented Jul 8, 2016 at 7:04
  • 1
    What's the logic here, why does it not add a line break at iOS 4? 4 is a number too. Commented Jul 8, 2016 at 7:12
  • Is the marker always 3 digits? Commented Jul 8, 2016 at 7:21
  • @cdarke Yes! How can I do this? Commented Jul 8, 2016 at 7:25

1 Answer 1

0

Assuming the number is always 3 digits (since you have other numbers embedded in the data):

data = "100Usable by Everybody: Design Principles for Accessibility on Mac OS X 101What's New in Cocoa Touch 102What's New in Foundation for iOS 4 103iPad and iPhone User Interface Design 104Designing Apps with Scroll Views 105Adopting Multitasking on iPhone OS, Part 1 106Understanding Document Interaction Controller 107Cocoa Tips and Tricks"

import re

new_data = re.sub(r'(\d{3})', '\n- \\1 ', data)
print(new_data)

Gives:

- 100 Usable by Everybody: Design Principles for Accessibility on Mac OS X 
- 101 What's New in Cocoa Touch 
- 102 What's New in Foundation for iOS 4 
- 103 iPad and iPhone User Interface Design 
- 104 Designing Apps with Scroll Views 
- 105 Adopting Multitasking on iPhone OS, Part 1 
- 106 Understanding Document Interaction Controller 
- 107 Cocoa Tips and Tricks
Sign up to request clarification or add additional context in comments.

3 Comments

So nice solution. Thank you very much.
OK, one caveat: you must guarantee that the text data does not contain 3 digits anywhere!
The text data always contain 3 digits. Thanks again.

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.