简体   繁体   English

正则表达式错误,匹配数字

[英]Error with regex, match numbers

I have a string 00000001001300000708303939313833313932E2 我有一个字符串00000001001300000708303939313833313932E2

so, I want to match everything between 708 & E2 .. 所以,我想匹配708 & E2之间的所有内容。

So I wrote: 所以我写道:

(?<=708)(.*\\n?)(?=E2) - tested in RegExr (it's working) (?<=708)(.*\\n?)(?=E2) -已在RegExr中测试(正在运行)

Now, from that result 303939313833313932 match to get result (every second number): 现在,从该结果303939313833313932匹配以获取结果(第二个数字):

099183192

How ? 怎么样 ?

To match everything between 708 and E2 , use: 要匹配708E2之间的所有内容,请使用:

708(\d+)

if you are sure that there will be only digits. 如果您确定只有数字。 Otherwise try with: 否则请尝试:

708(.*?)E2

To match every second digit from 303939313833313932 , use: 要匹配303939313833313932第二个数字,请使用:

(?:\d(\d))+

use a global replace: 使用全局替换:

find: \d(\d)
replace: $1

Are you expecting a regular expression answer to this? 您是否希望对此有一个正则表达式答案?

You are perhaps better off doing this using string operations in whatever programming language you're using. 您可能最好以使用任何编程语言的字符串操作来执行此操作。 If you have text = "abcdefghi..." then do output = text[0] + text[2] + text[4]... in a loop, until you run out of characters. 如果您有text = "abcdefghi..."那么请循环output = text[0] + text[2] + text[4]... ,直到用完字符为止。

You haven't specified a programming language, but in Python I would do something like: 您尚未指定编程语言,但是在Python中,我将执行以下操作:

>>> text = "abcdefghjiklmnop"
>>> for n, char in enumerate(text):
...   if n % 2 == 0: #every second char
...     print char
... 
a
c
e
g
j
k
m
o

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

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