这应该工作
REGEXP:对于Python和记事本++
(?:.*)(?:\blocalhost\/|localhost:4080\/)(.*)
REPLACE:
https://localhost:4080/$1
Python代码:
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?:.*)(?:\blocalhost\/|localhost:4080\/)(.*)"
test_str = ("https://localhost/subservice1 \n"
"https://localhost:4080/subservice1 \n"
"https://localhost/subservice1 \n"
"https://localhost/subservice2")
subst = "https://localhost:4080/$1"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
REGEXP:
INPUT:
https://localhost/subservice1
https://localhost:4080/subservice1
https://localhost/subservice1
https://localhost/subservice2
OUTPUT:
https://localhost:4080/subservice1
https://localhost:4080/subservice1
https://localhost:4080/subservice1
https://localhost:4080/subservice2
参见:https://regex101.com/r/kbPHd6/1
尝试Python代码:http://ideone.com/yNoYzv
如果我帮助ü,标志着我为正确答案和投票了。
说明:
(?: Non-capturing group. Groups multiple tokens together without creating a capture group.
. Dot. Matches any character except line breaks.
* Star. Match 0 or more of the preceding token.
)
(?: Non-capturing group. Groups multiple tokens together without creating a capture group.
\b Word boundary. Matches a word boundary position such as whitespace, punctuation, or the start/end of the string.
l Character. Matches a "l" character (char code 108).
o Character. Matches a "o" character (char code 111).
c Character. Matches a "c" character (char code 99).
a Character. Matches a "a" character (char code 97).
l Character. Matches a "l" character (char code 108).
h Character. Matches a "h" character (char code 104).
o Character. Matches a "o" character (char code 111).
s Character. Matches a "s" character (char code 115).
t Character. Matches a "t" character (char code 116).
\/ Escaped character. Matches a "/" character (char code 47).
| Alternation. Acts like a boolean OR. Matches the expression before or after the |.
l Character. Matches a "l" character (char code 108).
o Character. Matches a "o" character (char code 111).
c Character. Matches a "c" character (char code 99).
a Character. Matches a "a" character (char code 97).
l Character. Matches a "l" character (char code 108).
h Character. Matches a "h" character (char code 104).
o Character. Matches a "o" character (char code 111).
s Character. Matches a "s" character (char code 115).
t Character. Matches a "t" character (char code 116).
: Character. Matches a ":" character (char code 58).
4 Character. Matches a "4" character (char code 52).
0 Character. Matches a "0" character (char code 48).
8 Character. Matches a "8" character (char code 56).
0 Character. Matches a "0" character (char code 48).
\/ Escaped character. Matches a "/" character (char code 47).
)
(Capturing group #1. Groups multiple tokens together and creates a capture group for extracting a substring or using a backreference.
. Dot. Matches any character except line breaks.
* Star. Match 0 or more of the preceding token.
)
搜索'本地主机(:: 4080?)'和'与本地主机替换它:4080' –
只需更换'本地主机/'和'本地主机:4080 /' –
谢谢你所有的答案。所有的答案都是正确的。我改变了一些问题。我想改变我的配置文件中的所有端口。我忘了提及这一点。 – christoph