正則表達式匹配除兩個字符串之外的所有內容?

[英]Regular expression to match everything except two strings?


Let's say I have some logs saying:

假設我有一些日志:

s3://hello/world
s3n://hello/again

I want to get the following result:

我想得到以下結果:

hello/world
hello/again

My initial idea was ((s3n:\/\/)|(s3:\/\/)) but this, of course, selects just s3n:// and s3://. I need to negate (invert) this selection but it isn't as simple as it seems for strings (not characters).

我最初的想法是(s3n:\/\/)|(s3:\/\/)),但這當然只選擇了s3n:/和s3://。我需要否定(反轉)這個選擇,但它並不像字符串(不是字符)那樣簡單。

How would you go about inverting this selection?

你要怎么把這個選項倒轉過來呢?

2 个解决方案

#1


2  

I would suggest using ^s3n?:\/\/(.*), which can be tried here. Your desired string is in the 1st capturing group.

我建議使用^ s3n ?:\ / \ /(. *),可以在這里嘗試。你想要的字符串在第1捕獲組。

Explanation:

解釋:

  • ^ - Matches the start of the string.
  • ^ -匹配字符串的開始。
  • s3n? - Matches "s3" followed by an optional "n".
  • s3n嗎?-匹配“s3”,后跟一個可選的“n”。
  • :\/\/ - Matches a colon and two forward slashes.
  • :\/\/ -匹配一個冒號和兩個向前斜線。
  • (.*) - Matches and captures the rest of the string.
  • (.*)-匹配並捕獲字符串的其余部分。

#2


0  

You need to catch what comes after the semicolon with a catch group (parentheses). To avoid catching the "s3n" itself, you can add a ?: in the beginning of that group.

您需要用catch組(括號)捕獲分號后面的內容。為了避免捕獲“s3n”本身,您可以在該組的開頭添加一個?:。

(?:s3n:\/\/|s3:\/\/)(.*)

https://www.phpliveregex.com/p/oak

https://www.phpliveregex.com/p/oak


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2018/05/31/40713f9a98040f10f3aa939fe3dca537.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com