正则表达式无法识别“#”以从以“#”开头的单词中删除“#”

时间:2019-02-01 04:32:42

标签: python regex python-3.x data-science

如果它是单词中的第一个字符,如何从字符串中的单词中删除#。如果它本身存在,在单词的中间或在单词的末尾,则应该保留它。

当前我正在使用正则表达式:

test = "# #DataScience"
test = re.sub(r'\b#\w\w*\b', '', test) 

用于从#开头的单词中删除#,但是根本不起作用。它按原样返回字符串

谁能告诉我为什么#未被识别和删除?

示例-

test - "# #DataScience"
Expected Output - "# DataScience"

Test - "kjndjk#jnjkd"
Expected Output - "kjndjk#jnjkd"

Test - "# #DataScience #KJSBDKJ kjndjk#jnjkd #jkzcjkh# iusadhuish#""
Expected Output -"# DataScience KJSBDKJ kjndjk#jnjkd jkzcjkh# iusadhuish#"

3 个答案:

答案 0 :(得分:0)

您可以用空格' '分割字符串,以列出字符串中所有单词的列表。然后在该列表中循环,检查每个单词是否符合给定条件,并在必要时替换哈希。之后,您可以使用空格' '加入列表,以创建一个字符串并返回它。

def remove_hash(str):
    words = str.split(' ')  # Split the string into a list
    without_hash = []  # Create a list for saving the words after removing hash
    for word in words:
        if re.match('^#[a-zA-Z]+', word) is not None:  # check if the word starts with hash('#') and contains some characters after it.
            without_hash.append(word[1:])  # it true remove the hash and append it your the ther list
        else:
            without_hash.append(word)  # otherwise append the word as is in new list
    return ' '.join(without_hash)  # join the new list(without hash) by space and return it.

输出:

>>> remove_hash('# #DataScience')
'# DataScience'
>>> remove_hash('kjndjk#jnjkd')
'kjndjk#jnjkd'
>>> remove_hash("# #DataScience #KJSBDKJ kjndjk#jnjkd #jkzcjkh# iusadhuish#")
'# DataScience KJSBDKJ kjndjk#jnjkd jkzcjkh# iusadhuish#'

您可以避免以下类似情况,从而使代码更短(但更难理解):

def remove_hash(str):
words = str.split(' ' )
    without_hash = []
    for word in words:
        without_hash.append(re.sub(r'^#+(.+)', r'\1', word))
    return ' '.join(without_hash)

这将为您带来相同的结果

答案 1 :(得分:0)

请尝试以下模式。它查找位于字符串开头的一系列“#”和空格,并将其替换为“#”

import re

test = "# #DataScience"
test = re.sub(r'(^[#\s]+)', '# ', test)

>>>test
# DataScience

您可以在此处进一步使用该模式:PostgreSQL doc

答案 2 :(得分:0)

protected $table = 'coa_lvl1';

protected $primaryKey = 'lvl1';

public $incrementing = false;

protected $blameable  = array('created','updated');

protected $fillable = ['group_id','lvl1_code','lvl1_name','active_flag'];

public $timestamps = false;

public function coaGroup()
{
    return $this->belongsTo(CoaGroup::class, 'group_id');
}

public function coaLvl1()
{
    return $this->hasMany(CoaLvl2::class,'lvl1');
}