问题描述
如果我有一个大文本,我需要只保留匹配的内容,我该怎么做?
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
并使用此正则表达式:[0-9]([az])
将数字后的所有字母分组并替换为 \1
我将全部替换(数字)(letter) to (letter) (如果我想删除其余的并只保留匹配的字母)?...
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)?...
将此文本转换为
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
我可以匹配所有不是 [0-9]([a-z])
的吗?
Can i match all that is not [0-9]([a-z])
?
谢谢!:D
推荐答案
您可以使用以下正则表达式:
You may use the following regex:
(?i-s)[0-9]([a-z])|.
替换为 (?{1}$1:)
.
要删除除不匹配之外的所有内容,请使用具有相同正则表达式的 (?{1}$0:)
替换.
To delete all but non-matched, use the (?{1}$0:)
replacement with the same regex.
详情:
(?i-s)
- 内联修饰符打开不区分大小写模式并关闭 DOTALL 模式(.
与换行符不匹配)[0-9]([az])
- 一个 ASCII 数字和任何捕获到组 1 中的 ASCII 字母(以后用$1
或\1
来自字符串替换模式的反向引用)|
- 或.
- 除换行符以外的任何字符.
(?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.
更换详情
(?{1}
- 条件替换的开始:如果第 1 组匹配,则...$1
- 第 1 组的内容(如果使用$0
反向引用,则为整个匹配):
- 否则……什么都没有
(?{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
这篇关于删除所有内容但保持匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!