2017-01-27 56 views
0

我有一個大文件,其中包含localhost和localhost:portnumber(端口號不是constant.it可以是4070,8080,9090)的幾行。我想將所有localhost或localhost:portnumber改爲localhost:4080.但我不想改變localhost:4080.Simple search and replace(localhost to localhost:4080)will localhost:portnumber to localhost:4080:portnumber.Any的方式在記事本++與regex.Preferbly爲此正則表達式來改變一個字符串,但不是字符串+ string2

例如輸入:

https://localhost/subservice1 
https://localhost:4080/subservice1 
https://localhost:1090/subservice1 
https://localhost/subservice2 

輸出應該是

https://localhost:4080/subservice1 
https://localhost:4080/subservice1 
https://localhost:4080/subservice1 
https://localhost:4080/subservice2 
+0

搜索'本地主機(:: 4080?)'和'與本地主機替換它:4080' –

+0

只需更換'本地主機/'和'本地主機:4080 /' –

+0

謝謝你所有的答案。所有的答案都是正確的。我改變了一些問題。我想改變我的配置文件中的所有端口。我忘了提及這一點。 – christoph

回答

2

這應該工作

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. 
) 
+0

非常感謝。我將你的答案標明爲正確。你可以解釋一下你的解決方案,我編輯了一下我的問題。請你告訴我,如果可以這樣做的話。 – christoph

+0

對不起,我不知道如何解釋它,但添加了語法條約如何完成的細節。 PD:不客氣。 –

1

更換://localhost/://localhost:4080/

1
import re 

text = [ 
    "https://localhost/subservice1", 
    "https://localhost:4080/subservice1", 
    "https://localhost/subservice1", 
    "https://localhost/subservice2" 
] 

regex = r'localhost/' 
for x in text: 
    result = re.sub(regex, "localhost:4080/", x) 
    print(result) 
相關問題