简体   繁体   中英

RegEX: How to exclude between string and to match the renaming string

I did make the inverse, match between two string and exclude the remaining, but I'm not getting deny the first sentence.

String:

iso.3.6.1.4.1.25355.3.2.6.4.2.5.1.7.3.2.11.19 = Hex-STRING: 14 CC 20 B7 F5 D1 70 4F 57 4C D3 95
iso.3.6.1.4.1.25355.3.2.6.4.2.5.1.7.3.2.12.21 = Hex-STRING: 18 A6 F7 80 27 6F C4 E9 84 A8 B9 B1 18 A6 F7 80
32 B3
iso.3.6.1.4.1.25355.3.2.6.4.2.5.1.7.3.2.13.22 = Hex-STRING: 30 B5 C2 CE 55 81 C4 E9 84 BD 58 E5
iso.3.6.1.4.1.25355.3.2.6.4.2.5.1.7.3.2.16.25 = Hex-STRING: 18 A6 F7 65 A5 75 C4 E9 84 CB CC 61 B0 4E 26 8B
4E E5 B0 4E 26 8B 41 E7 84 16 F9 92 EA CB C4 E9
84 A8 C1 3D
iso.3.6.1.4.1.25355.3.2.6.4.2.5.1.7.3.2.17.29 = Hex-STRING: 70 4F 57 4C CB FF 70 4F 57 39 5E 33
iso.3.6.1.4.1.25355.3.2.6.4.2.5.1.7.3.2.18.38 = Hex-STRING: 98 DE D0 D3 4E 7D 18 A6 F7 80 3D 41 84 16 F9 AF
E2 AF 9C 6F 52 12 08 A7

RegEX: (?=iso)(.*)(?<=:)

Result: Matching iso... STRING: . I would like to match all hexadecimal address.

You may use

import re

s = "YOUR_DATA"
results = []
for match in re.findall(r'Hex-STRING:(?:\s+[\dA-Fa-f]{2})+', s):
    results.append([" ".join(x.split()) for x in re.findall(r'[\dA-Fa-f]{2}(?:\s+[\dA-Fa-f]{2}){5}', match)])
print(results)

Output:

[['14 CC 20 B7 F5 D1', '70 4F 57 4C D3 95'], ['18 A6 F7 80 27 6F', 'C4 E9 84 A8 B9 B1', '18 A6 F7 80 32 B3'], ['30 B5 C2 CE 55 81', 'C4 E9 84 BD 58 E5'], ['18 A6 F7 65 A5 75', 'C4 E9 84 CB CC 61', 'B0 4E 26 8B 4E E5', 'B0 4E 26 8B 41 E7', '84 16 F9 92 EA CB', 'C4 E9 84 A8 C1 3D'], ['70 4F 57 4C CB FF', '70 4F 57 39 5E 33'], ['98 DE D0 D3 4E 7D', '18 A6 F7 80 3D 41', '84 16 F9 AF E2 AF', '9C 6F 52 12 08 A7']]

See the Python demo .

The Hex-STRING:(?:\\s+[\\dA-Fa-f]{2})+ pattern will match Hex-STRING: and then 1 or more repetitions of 1+ whitespaces and then 2 hex chars, as many times as possible. These matches will serve as input for the second regex, [\\dA-Fa-f]{2}(?:\\s+[\\dA-Fa-f]{2}){5} , that will extract all the expected results from those pre-extracted strings.

The [\\dA-Fa-f]{2}(?:\\s+[\\dA-Fa-f]{2}){5} pattern matches two hex chars with [\\dA-Fa-f]{2} and (?:\\s+[\\dA-Fa-f]{2}){5} matches five consecutive occurrences of 1+ whitespaces followed with two hex chars.

The [" ".join(x.split()) for x in re.findall(...)] list comprehension helps replace line breaks between hex pairs with a single space.

If you're just trying to match the hex values at the end of each line, something like this should be sufficient:

Hex-STRING:\\s*(.*)$

This will match Hex-STRING followed by any whitespace, and then capture everything until the end of the line.

Here's an example using your provided input: https://regex101.com/r/1TKKcf/2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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