How to get the first word regex

I am new to regex and am trying to figure this out:

This is some text 1
This is some text 2
This is some awesome text
Test!
Test!
This is some text 3
This is some text 4
This is some awesome text

Here are a few lines of text, I try to get everything from "This is some kind of text 1" right up to the first "Test!". But I keep getting it in the second β€œTest!”. How can I do this just to capture the first "test!" ??

Here is what I did:

(.*)(Test!)

In principle, everything turns out, and then stops on the Test! (At first I thought that I stopped at the first) Then I tried:

(.*)(Test!)[\f\r\t\n]

Still got the second one, I just included everything else. Then I tried:

(.*)(Test!){1}

Thought I'd get the first and only that.

+4
source share
1 answer

Regex , , . ?, :

(.+?)Test!
   ^ make it non-greedy

Stub. s . .

+2

Source: https://habr.com/ru/post/1617773/


All Articles