大佬们怎么用正则定位到 这样的内容 XXXXXX : 8

2020-09-23 11:16:46 +08:00
 AzsharR
processor : 0
BogoMIPS : 38.40
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x51
CPU architecture: 8
CPU variant : 0x2
CPU part : 0x201
CPU revision : 1

processor : 1
BogoMIPS : 38.40
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x51
CPU architecture: 8
CPU variant : 0x2
CPU part : 0x201
CPU revision : 1


就是定位到上面这样的

下面是我自己写的
re.findall(r'processor[\s\w\d]{1}', a)
结果是['processor\t', 'processor\t', 'processor\t', 'processor\t']
并定位不到 完整的内容
1832 次点击
所在节点    Python
9 条回复
forty
2020-09-23 11:22:26 +08:00
定啥定啊,直接按连续 2 个换行符分割不就行了吗?
Rxianbei
2020-09-23 11:23:29 +08:00
@forty 很有道理
umissthestars
2020-09-23 11:31:02 +08:00
/(\n.*?architecture\s?[::]\s?8.*?)(?!\n)/gi


// example
```
const str = `processor : 0 BogoMIPS : 38.40 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 CPU implementer : 0x51 CPU architecture: 8 CPU variant : 0x2 CPU part : 0x201 CPU revision : 1

processor : 1 BogoMIPS : 38.40 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 CPU implementer : 0x51 CPU architecture: 8 CPU variant : 0x2 CPU part : 0x201 CPU revision : 1

processor : 2 BogoMIPS : 38.40 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 CPU implementer : 0x51 CPU architecture: 8 CPU variant : 0x2 CPU part : 0x205 CPU revision : 1

processor : 3 BogoMIPS : 38.40 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 CPU implementer : 0x51 CPU architecture: 8 CPU variant : 0x2 CPU part : 0x205 CPU revision : 1`
```
// output
```
["↵processor : 1 BogoMIPS : 38.40 Features : fp asim… crc32 CPU implementer : 0x51 CPU architecture: 8", "↵processor : 2 BogoMIPS : 38.40 Features : fp asim… crc32 CPU implementer : 0x51 CPU architecture: 8", "↵processor : 3 BogoMIPS : 38.40 Features : fp asim… crc32 CPU implementer : 0x51 CPU architecture: 8"]
```
qazwsxkevin
2020-09-23 11:31:04 +08:00
import re
tarString = "CPU architecture: 8"
getStr = re.sub('(?<=:).*?(?=$")', 'utf-8', tarString)

# 方法 2,必定是数字的话
getStr = re.findall(r"\d+",tarString)
AzsharR
2020-09-23 11:32:46 +08:00
@forty 因为每次,内容不同,所以需要定位 processor 这个关键词查看后面内容,但是又因为 processor 后面又很多空格定位不到
umissthestars
2020-09-23 11:33:53 +08:00
你这输入和我进来时的不一样了...你又偷偷更新了
AzsharR
2020-09-23 11:36:06 +08:00
@umissthestars 别不好意思 因为呈现出来的格式打乱了 改了一下
caaaalabash
2020-09-23 16:39:31 +08:00
/(processor.+?)(?=processor)|(processor.+)(?=$)/sg
wd941113
2020-09-29 10:50:40 +08:00
string = "processor:****"
base_string_split = [{i.split(":")[0].strip(): i.split(":")[1].strip() for i in temp.strip("\n").split("\n")} for temp in string.split("\n\n")]
for string_temp in base_string_split:
processor = string_temp.get(processor)

--over--

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/709695

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX