简体   繁体   English

正则表达式以提取特定分号后的数字

[英]Regular expression to extract numbers after a specific semicolon

Can you help me to write an expression for the following string: 您能帮我为以下字符串写一个表达式吗?

Multi 3620 IDS; 17120846;9;12.04.2018 
14:09:02;8,531;;pH;24,1;°C;Temp;AR;60%;;SenTix 940; C171412055;

How to get just the number 8,531 using a regular expression? 如何使用正则表达式仅获取数字8,531

Is there any rule to extract the numbers after a specific number of semicolons? 是否有任何规则在特定数量的分号后提取数字?

Thank you 谢谢

No regex solution 没有正则表达式解决方案

Personally, I wouldn't even use regex for this: 就个人而言,我什至不使用正则表达式:

See code in use here 在这里查看正在使用的代码

import re

s = "Multi 3620 IDS; 17120846;9;12.04.2018 \n14:09:02;8,531;;pH;24,1;°C;Temp;AR;60%;;SenTix 940; C171412055;"
print(s.split(";")[4])

Regex solution 正则表达式解决方案

But if you must use regex (for some unknown reason) you can use the following 但是,如果必须使用正则表达式(出于某些未知原因),则可以使用以下命令

See regex in use here 查看正则表达式在这里使用

(?:[^;]*;){4}([^;]+)
  • (?:[^;]*;){4} Match the following exactly 4 times (?:[^;]*;){4}准确匹配以下4次
    • [^;]* Match any character except ; [^;]*匹配除;以外的任何字符 any number of times 任何次数
    • ; Match this literally 从字面上匹配
  • ([^;]+) Capture any character except ; ([^;]+)捕获除;以外的任何字符; one or more times into capture group 1 进入捕获组1一次或多次

See code in use here 在这里查看正在使用的代码

import re

s = "Multi 3620 IDS; 17120846;9;12.04.2018 \n14:09:02;8,531;;pH;24,1;°C;Temp;AR;60%;;SenTix 940; C171412055;"
r = re.compile("(?:[^;]*;){4}([^;]+)")
m = r.match(s)
if m:
    print(m.group(1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM