2017-01-27 55 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) 
相关问题