Regex - delete everything but matching groups (using sublime 3) - regex

How can I keep only matching groups and delete the rest of the text?
Using: Sublime 3 - Regex
My pattern is
1.5.1 Bla bla bla
text text text
text text text
1.5.2 Bla bla bla
text text text
I want to keep only this
1.5.1 Bla bla bla
1.5.2 Bla bla bla
I can manage to select only the groups, but not everything except them.
Link: https://regex101.com/r/pV9xU6/2
Thank you

According to the comments, it can be done in several ways:
Find: (?s)^(1\.5\.\d+[^\n]*\n[^\n]*\n)|. /gm
Replace: $1
or
Find (general way): (*SKIP)(*F)|.*\R*
Find: (1[.]5[.]\d+.*\n.*\n)(*SKIP)(*F)|.*\R*
Replace: nothing
or
Find: (^1\.5\.\d+.*\n.*\n)\K(?>.*\R)*?(?=(?1)|.*\z) /gm
Replace: nothing
Thanks for all your help.

Related

Markdown inside an if statement with Streamlit

I am trying to print a paragraph with st.markdown in Streamlit. It was working correctly before putting it inside the if statement but now I only get a code cell:
if LOGGED_IN == True:
st.markdown('''___\n
bla bla bla
___''')
You need to remove the indentation before the markdown text. This works:
if LOGGED_IN is True:
st.markdown('''___\n
bla bla bla
___''')

Replace matching pairs of characters with parenthesis

It is better to describe an example. I have a latex source file (this is an ordinary text file) that has a lot of charactes $ enclosing inline equations, something like this:
bla bla bla $E = mc^2$ bla blah
I would like to replace each ocurrence of a matching pair of $ characters in the file by \( ... \), like this:
bla bla bla \(E = mc^2\) bla blah
Any idea of to do this, as simple as possible? I am not sure grep is able to handle this.
Assume that the file has an even number of occurrences of $. In that case, all we have to do is replace the $ at odd positions by \(, and the $ at even positions by \).
Like this?
spacewrench$ cat foo
bla bla bla $E = mc^2$ bla blah
spacewrench$ sed -e 's/\$\(.*\)\$/\\(\1\\)/g' < foo
bla bla bla \(E = mc^2\) bla blah
sed can do it. You may need to play with the number of backslashes, plus line endings if you have expressions that extend over multiple lines.
The .* expression is greedy, so it might only put one pair of parentheses around multiple $ on a line...you can fix that by replacing .* with [^\$]*.

Regex: replace newline except the ones with a dot

Thanks in advance for any help you can provide
I have a text like that:
Bla bla bla bl[CR][LF]
a bla bla bla[CR][LF]
bla bla.[CR][LF]
Bla bla bla bla bl[CR][LF]
...and so on
I'd like to replace all new lines except the ones having a dot as last character.
This is the what I wanna get to:
Bla bla bla bla bla bla bla bla bla.[CR][LF]
Bla bla bla bla bla.[CR][LF]
...and so on
I tried with Notepad++, that supports RegEx, using the Search & Replace tab (Ctrl+H). That's the code:
Search: [^.\r\n]\r\n
Replace field had just a space.
It worked, but it truncates the last character of every line.
Bla bla bla bla bla bla bla bla bl.[CR][LF]
Bla bla bla bla bl.[CR][LF]
As I am a RegEx novice, which is the best way to do that?
Use this regex: (?<!\.)\r\n in the search field. It means find any \r\n that isn't preceded by a ..
Your regex means find any three characters where the first one isn't a . \r or \n, and the last two are \r\n. But then when you go to replace, it replaces that 1st character as well. The regex I posted checks for the non-period as a zero-length, so it doesn't replace that character.

Regexp how do a match a not equal

Data
Bla bla usr/bin/rcp bla bla
Bla bla usr/bin/awl bla bla
Bla bla usr/bin/cp bla bla
Bla bla usr/bin/ftp bla bla
Bla bla usr/bin/cut bla bla
Ignore list
cp
ftp
rcp
Problem
I Need a regular expression (Java ish) that will process the data lines (of which there will be many others) and if usr/bin/ is found show it as a match but only if not followed by a word on the ignore list
Please see Regex Demo here.
usr\/bin\/(?!cp|ftp|rcp)
You need a negative lookahead regex here, try this regex:
user/bin/(?!(cp|ftp|rcp))

Regular expression to find text between strings WITHOUT a predefined string in the middle

I've a so written text:
11 bla gulp bla 22
11 bla bla bla 2211 bla
ble
bli 22
I need a regex to find all the text between all the couples "11" and "22" BUT that DON'T contain "gulp".
If I search (?s)11.*?22 using TextCrawler, I find all the three strings:
bla gulp bla
bla bla bla
bla ble bli
Wrong! I'd like to obtain only:
bla bla bla
bla ble bli
because "bla gulp bla" contains "gulp", and I don't want it!
Any idea? :-)
use a negative lookahead assertion:
11(?!.*?gulp.*?)(.*?)22
word boundaries might be a good idea in the middle (surrounding gulp), because it would allow to distinguish between gulp and gulping, gulped or ungulp(?):
11(?!.*?\bgulp\b.*?)(.*?)22
but putting them around everything:
\b11\b(?!.*?\bgulp\b.*?)(.*?)\b22\b
would exclude your other two results - not what you want.