Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all content but keeping matched

If i have a big text, and i'm needind to keep only matched content, how can i do that?

For example, if I have a text like this:

asdas8Isd8m8Td8r
asdia8y8dasd
asd8is88n8gd
asd8t8od8lsdas
as9ea9ad8r1n88r8e87g6765ejasdm8x

And use this regex: [0-9]([a-z]) to group all letters after a number and replace with \1 i will repace all (number)(letter) to (letter) (And if i want to delete the rest and stay only with the letter matched)?...

Converting this text to

ImTr
y
ing
tol
earnregex

How can i replace this text with grouped and delete the rest?

And if i want to delete all but no matched? In this case, converting the text to:

8I8m8T8r
8y8d
8i8n8g
8t8o8l
9e9a9r1n8r7g5e8x

Can i match all that is not [0-9]([a-z])?

Thanks! :D

like image 437
Emanuel Avatar asked Oct 15 '25 22:10

Emanuel


1 Answers

You may use the following regex:

(?i-s)[0-9]([a-z])|.

Replace with (?{1}$1:).

To delete all but non-matched, use the (?{1}$0:) replacement with the same regex.

Details:

  • (?i-s) - an inline modifier turning on case insensitive mode and turning off the DOTALL mode (. does not match a newline)
  • [0-9]([a-z]) - an ASCII digit and any ASCII letter captured into Group 1 (later referred to with $1 or \1 backreference from the string replacement pattern)
  • | - or
  • . - any char but a line break char.

Replacement details

  • (?{1} - start of the conditional replacement: if Group 1 matched then...
    • $1 - the contents of Group 1 (or the whole match if $0 backreference is used)
    • : - else... nothing
  • ) - end of the conditional replacement pattern.

enter image description here

like image 146
Wiktor Stribiżew Avatar answered Oct 18 '25 12:10

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!