Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
101 views
in Technique[技术] by (71.8m points)

How to get part of data with Regex

I have an example which is :

[Unit] = 
[Diag] = Nilan zok-98
Alert danger C-00N47

[Unit] = 17B NORWAY 
[Diag] = Mobari-testing-17-AB
Pair num to get 17-9881 to be testing
Class nump

[Unit] = 
[Diag] = BLOC1 

I want to extract this example in two part like as below :

Match 1
Group 1.    [Unit] = 
Group 2.    [Diag] = Nilan zok-98
Alert danger C-00N47

Match 2
Group 1.    [Unit] = 17B NORWAY 
Group 2.    [Diag] = Mobari-testing-17-AB
            Pair num to get 17-9881 to be testing
            Class nump

Match 3
Group 1.    [Unit] = 
Group 2.    [Diag] = BLOC1 

I was using (.*[Unit].* ).*(.*[Diag].*) to get solution, but Diag section get always first line.

You find my test regex here : https://regex101.com/r/p6nLiK/1

question from:https://stackoverflow.com/questions/65904685/how-to-get-part-of-data-with-regex

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use

([Unit].*)
([Diag].*(?:
.+)*)

See the regex demo

Details:

  • ([Unit].*) - Group 1: [Unit] and the rest of the line
  • - a line feed char
  • ([Diag].*(?: .+)*) - Group 2: [Diag], then the rest of the line and then zero or more non-empty lines.

A variation of the pattern is

(?m)^([Unit].*)
([Diag].*(?:
h*S.*)*)

See this regex demo.

Here, (?m)^ matches any start of line position and h*S.* makes it match non-blank lines (h* matches zero or more horizontal whitespace and S matches any non-whitespace char). Replace h with [p{Zs}] or [^S ] if not supported.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...