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

regex - Match newlines in non empty lines and not preceded by some characters

I tried to use the regexp (?<=^(?![ ]*@).+)(?<![{};:)]|//.{0,1024}|^[ ]+)R to:

  • Match newlines without matching preceding characters
  • Don't match newlines in only-space lines (empty lines)
  • Don't match newlines in lines started by optional spaces (not newlines) and at-symbol '@'.
  • Don't match newlines in lines containing [unescaped] '//'.
  • Don't match newlines preceded by '{', '}', ';' or ':'. (better if doesn't match newlines preceded by optional spaces and that characters.)

That regexp worked in Eclipse search, but I cannot replace the match because an Eclipse find-and-replace bug. Then I tried it in Notepad++ and in TextPad, but they told me that the regexp is malformed. I tried to validate the regexp in a online regexp validator and said me the regex has errors.

How do I construct a correct regexp that satisfy those listed requirements?

has to match the new line.
    has to match the new line.
has to match the new line
doesn't have to match the new line;
    doesn't have match the new line     {
doesn't have to match the new line}
doesn't have to match the new line:
doesn't have to match the new line //because of this
@doesn't have to match the new line
h@s to match the new line-
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You can give a try to this regex ^[^@]+[ ]*.*[ ]*([^;{}:]+)[ ]*$

It will cover you all cases except below one

doesn't have to match the new line //because of this


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

...