From a large list of text I want to get only these lines:
Ks 2 ... No brackets at the end!
Ks 2 ... (/R)
So from an example as below:
Ks 2 an wo co lo
Ks 2 ni ta ko shi (/R)
Ks 2 fa ki de so (R)
Ks 1 ha lo pa
Pattern should match:
Ks 2 an wo co lo
Ks 2 ni ta ko shi (/R)
I have tried to create something like this: Ks 2(.*)\(\/R\)|Ks 2(.*)(^(\(\/R\))$
Can anyone help me how to create this pattern ?!
CodePudding user response:
You can use
^Ks 2.*(?:\(/R\)|[^)])$
^Ks 2.*(?:\(/R\)|(?<!\)))$
See this regex demo (or this demo).
Note: If you deal with multiline texts, add \r?
before $
and add (?m)
at the start of the pattern. Also, replace [^)]
with [^)\n]
in the first pattern. E.g. (?m)^Ks 2.*(?:\(/R\)|[^)\r\n])\r?$
and (?m)^Ks 2.*(?:\(/R\)|(?<!\)\r?))\r?$
.
Details:
^
- start of a stringKs 2
- a fixed string.*
- any zero or more chars other than line break chars as many as possible(?:\(/R\)|[^)])
- either(/R)
or a char other than)
$
- end of string.