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
256 views
in Technique[技术] by (71.8m points)

Complex regex get closest

I would like some help to finish my complex regex.

I spent some times on it and still can't figure out how I can achieve what I want

This is the text I want to parse :

Do [|83]([]?([]?([]?([]?([]?([]?([]?:))))):)([]?([]?:([]?:)):)([]?[]? :):)([]?([]?[]:):)([]?([]?[]:):) 
Bo [|18] pz ([]?:)
 la :
[pl] 
Co [|76] pp ([]?:)

For readability, I put every text in one line only but please consider that they are not on a new line.

This is my regex so far :

([|(d*)])+(?!\ ).*([%sa-zA-Z]*)((([[^[]()?:]*])+s*?([^()]*):([^()]*)))

I'm reading every combinations of [|NUMBER] () one by one. The process I apply on "()" depends of the NUMBER related

When I'm parsing the first time, I'm getting this which is fine :

enter image description here

Then, I replace the whole value after my process :

Now, I do have :

Do [|83] blabla done Bo [|18] pz ([]?:)
 la :
[pl]  Co [|76] pp ([]?:)

When I parse them once more, I got :

enter image description here

The number I got is not the good one. My question is : how can I get the closest one from the string I'm parsing after?

Thanks you for any tips

question from:https://stackoverflow.com/questions/66054645/complex-regex-get-closest

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

1 Reply

0 votes
by (71.8m points)

You might shorten the pattern a bit and exclude matching both the square brackets and the parenthesis in the character class after matching the digit and ]

[|d+][^][()]*([^()]*)

The pattern matches:

  • [|d+] Match [| 1+ digits and ]
  • [^][()]* Match 0+ times any char other than [ ] ( )
  • ([^()]*) Match (, than 0+ times any char other than ( ) then match )

Regex demo


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

...