我想在字符串中用下划线替换空白,以创建良好的url。例如:
"This should be connected"
应该成为
"This_should_be_connected"
我使用Python和Django。这可以用正则表达式解决吗?
我想在字符串中用下划线替换空白,以创建良好的url。例如:
"This should be connected"
应该成为
"This_should_be_connected"
我使用Python和Django。这可以用正则表达式解决吗?
当前回答
替换空格是可以的,但我建议进一步处理其他不利于url的字符,如问号、撇号、感叹号等。
还要注意,搜索引擎优化专家的普遍共识是,在url中,破折号比下划线更受欢迎。
import re
def urlify(s):
# Remove all non-word characters (everything except numbers and letters)
s = re.sub(r"[^\w\s]", '', s)
# Replace all runs of whitespace with a single dash
s = re.sub(r"\s+", '-', s)
return s
# Prints: I-cant-get-no-satisfaction"
print(urlify("I can't get no satisfaction!"))
其他回答
perl -e 'map { $on=$_; s/ /_/; rename($on, $_) or warn $!; } <*>;'
匹配和替换当前目录下所有文件的空格>下划线
令人惊讶的是,这个库还没有被提及
名为Python -slugify的Python包,它可以很好地进行slugify:
pip install python-slugify
工作原理如下:
from slugify import slugify
txt = "This is a test ---"
r = slugify(txt)
self.assertEquals(r, "this-is-a-test")
txt = "This -- is a ## test ---"
r = slugify(txt)
self.assertEquals(r, "this-is-a-test")
txt = 'C\'est déjà l\'été.'
r = slugify(txt)
self.assertEquals(r, "cest-deja-lete")
txt = 'Nín hǎo. Wǒ shì zhōng guó rén'
r = slugify(txt)
self.assertEquals(r, "nin-hao-wo-shi-zhong-guo-ren")
txt = 'Компьютер'
r = slugify(txt)
self.assertEquals(r, "kompiuter")
txt = 'jaja---lol-méméméoo--a'
r = slugify(txt)
self.assertEquals(r, "jaja-lol-mememeoo-a")
x = re.sub("\s", "_", txt)
替换空格是可以的,但我建议进一步处理其他不利于url的字符,如问号、撇号、感叹号等。
还要注意,搜索引擎优化专家的普遍共识是,在url中,破折号比下划线更受欢迎。
import re
def urlify(s):
# Remove all non-word characters (everything except numbers and letters)
s = re.sub(r"[^\w\s]", '', s)
# Replace all runs of whitespace with a single dash
s = re.sub(r"\s+", '-', s)
return s
# Prints: I-cant-get-no-satisfaction"
print(urlify("I can't get no satisfaction!"))
OP使用python,但在javascript中(这是要小心的,因为语法是相似的。
// only replaces the first instance of ' ' with '_'
"one two three".replace(' ', '_');
=> "one_two three"
// replaces all instances of ' ' with '_'
"one two three".replace(/\s/g, '_');
=> "one_two_three"