EmEditor (text editor) Forum Index
   EmEditor Core Enhancement Suggestions
     Ignore \r in regex
Register To Post

Flat Previous Topic | Next Topic
Poster Thread
CrashNBurn
Posted on: 9/22/2011 3:37 pm
Just can't stay away
Joined: 4/8/2010
From:
Posts: 130
Re: Ignore \r in regex
The (? syntax is something entirely different.

Example, given the following Text
Quote:
This is a test sentence
This is a test sentence
of a regex string
of a regex string


Try this regex:
Search: ^(.*)(?|(a test )|(a regex ))(.*)$
Replace: \3

With the (?| syntax, there will only be 3 pieces, since the section inside (?|...) isn't counted as a bracket set.
For the first line:
\1: This is_
\2: a test_
\3: sentence

The lines will be replaced with "sentence" and "string".


Now without the (?| syntax, eg:
Search: ^(.*)((a test )|(a regex ))(.*)$ that regex would contain 5 pieces that would be matched with \1 thru \5, and some of them would be empty, depending on the line.

For the first line:
\1: This is_
\2: a test_
\3: a test_
\4:
\5: sentence

The (? syntax becomes important if you utilize more complex regexes due to the nature of regex to match everything it absolutely can when things like ".*" are present.


Example 2A:
Search: ^(.*)( is a | a regex )?
Replace: \1_

That regex will match the whole line, and the replace wont change anything -- as the .* consumes the whole line, since the part in (brackets)? is optional, the greedy regex wont see it.


Example 2B:
Search: ^(.*)(?|( is a )|( a regex ))
Replace: \1_

This regex will actually remove " is a " and " a regex " from the lines in question.

A lot of editors don't even implement the full regex spec, e.g. Notepad2 does a small subset which doesn't even handle the optional "?" correctly, and certainly doesn't support the (?| syntax.
Flat Previous Topic | Next Topic


Subject Poster Date
   Ignore \r in regex Deipotent 8/24/2011 9:10 am
     Re: Ignore \r in regex Stefan 8/24/2011 12:02 pm
       Re: Ignore \r in regex Deipotent 8/24/2011 5:08 pm
         Re: Ignore \r in regex Stefan 8/25/2011 4:54 am
           Re: Ignore \r in regex Deipotent 8/25/2011 6:20 am
             Re: Ignore \r in regex Deipotent 8/25/2011 9:46 am
             » Re: Ignore \r in regex CrashNBurn 9/22/2011 3:37 pm

Register To Post
 
English čeština Deutsch español français italiano 日本語 한국어 Русский 简体中文 繁體中文