有一个像这样的字符串
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
我想找到所有数字子串,所以我得到一组数字字符串,其中一个将像'100'。然后我修改它,追加' \ n-'在它之前。所以最后我会得到一个这样的字符串
- 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
我如何使用python执行此操作,请帮忙。非常感谢。
找到数字子串后,我就用它了。例如,我找到' 100'然后它将替换为' \ n - 100'。
答案 0 :(得分:0)
假设数字总是3位数(因为数据中嵌入了其他数字):
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)
给出:
- 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