标签: string-matching

python中的字符串匹配与re

我有一个这种结构的文件:

009/foo/bar/hi23123/foo/bar231123/foo/bar/yo232131

我需要的是找到一个字符串的完全匹配; 例如/ foo/bar/hi和/ foo/bar/yo中只有/ foo/bar

我想到的一个解决方案就是检查输入字符串的结尾"/".因为如果可能的结果中有"/"结尾,这意味着它只有/ foo/bar不同.

对于这个解决方案,我必须说:

input = /foo/bar
Run Code Online (Sandbox Code Playgroud)

match input without ending "/"
Run Code Online (Sandbox Code Playgroud)

我怎么能在python中使用Regex做到这一点?

顺便问一下,如果有其他解决方案的建议,欢迎您在这里分享.

python regex string-matching

0
推荐指数
1
解决办法
1187
查看次数

Ruby:从字符串"相同字符串的部分"获取 - 介于它之间

我有一个字符串数组

["2 192.168.1.101 10000",56 192.168.1.101 10001","3 192.168.1.101 10002","1 192.168.1.101 10003"]
Run Code Online (Sandbox Code Playgroud)

我想获取部分或每个字符串项,并创建单独的数组,如:

["2","56","3","1"] , ["192.168.1.101","192.168.1.101","192.168.1.101","192.168.1.101"] and
["10000","10001","10002","10003"]
Run Code Online (Sandbox Code Playgroud)

我的问题是如何获得这些角色?

对于第一个数组,它会是从第一个到"空白空间得到<和第二个数组,它会是从""到""得到<和第三个类似于从"到最后得到<

我希望我的问题得到很好的理解,我得到一些回复,谢谢你提前!

ruby string rubygems ruby-on-rails string-matching

0
推荐指数
1
解决办法
162
查看次数

0
推荐指数
1
解决办法
68
查看次数

无论值如何,脚本都返回false

我正在创建一个程序,确认给出的核苷酸序列是否是回文序列.该脚本创建一个反向补码并将其与原始序列进行比较,确认如果2匹配则它是回文.问题是我的脚本将始终声明它不是回文,即使它是.

#!/usr/bin/perl
use strict;

print "Enter the sequence\n";
my $seq = <STDIN>;

my $r=reverse($seq);

$r =~ tr/ACTGactg/TGACtgac/;

print "Reverse complement: $r \n";

if ($r eq $seq) {
    print "The sequence is a palindrome\n";
} else {
    print "The sequence is NOT a palindrome\n";
}
Run Code Online (Sandbox Code Playgroud)

预期产出示例:

Enter the sequence:
CG
Reverse complement:
CG
The sequence is a palindrome
Run Code Online (Sandbox Code Playgroud)

perl palindrome string-matching dna-sequence

0
推荐指数
1
解决办法
32
查看次数

Javascript 和 Regex - 字符串中单词的严格匹配

我有一个这样的字符串:

Free-coffee and coffee-free is free coffee
Run Code Online (Sandbox Code Playgroud)

我只需要匹配和替换单独的单词free而不是单词{Free}-coffeecoffee-{free}

想法是在字符串中标记坏词并添加这样的HTML标签<strong>

Free-coffee and coffee-free is <strong>free<strong> coffee.
Run Code Online (Sandbox Code Playgroud)

我尝试使用空格,但如果在这句话之前有空格,有时会失败。

这是我当前的正则表达式:

/(\sfree\s|\sfree|free\s)/ig
Run Code Online (Sandbox Code Playgroud)

注意:这需要不区分大小写。

这是代码示例:

var text = "Free-coffee and coffee-free is free coffee";
text = text.replace(/(\sfree\s|\sfree|free\s)/ig, " <strong>strong</strong> ");
Run Code Online (Sandbox Code Playgroud)

请帮帮我。

javascript regex matching string-matching

0
推荐指数
1
解决办法
619
查看次数

如何在 Python 中使用正则表达式动态匹配整个单词

使用正则表达式,我想完全在 Python 中匹配一系列单词。静态是可能的,但我不知道动态匹配方式。

静态方法

import re
print(re.search(r'\bsmaller than or equal\b', 'When the loan amount is smaller than or equal to 50000'))
Run Code Online (Sandbox Code Playgroud)

我试图通过将整个序列与列表匹配来动态地做同样的事情。
这是下面的代码片段:

import re
list_less_than_or_equal = ['less than or equal', 'lesser than or equal', 'lower than or equal', 'smaller than or equal','less than or equals', 'lesser than or equals', 'lower than or equals', 'smaller than or equals', 'less than equal', 'lesser than equal', 'higher than equal','less than equals', 'lesser than equals', 'higher than equals']

for word in list_less_than_or_equal:
    print(re.search(r'\b'+word+'\b', …
Run Code Online (Sandbox Code Playgroud)

python regex string-matching

0
推荐指数
1
解决办法
1275
查看次数

从同一行中提取以特定字符开头的字符串

首先,如果我在发布之前没有遇到类似问题的答案,那么首先道歉.我有一组(72)基因注释文件.我想以下面的格式提取GO术语(奖金将是其他注释术语)

HORVU1Hr1G002090           GO:0003824
HORVU1Hr1G002090           GO:0006527
HORVU1Hr1G002090           GO:0008295
HORVU1Hr1G002090           GO:0008792
HORVU1Hr1G005360           GO:0004497
HORVU1Hr1G005360           GO:0005506
HORVU1Hr1G005360           GO:0016705
HORVU1Hr1G005360           GO:0020037
HORVU1Hr1G005360           GO:0055114
HORVU1Hr1G087600           GO:0009055
HORVU1Hr1G087600           GO:0015035
HORVU1Hr1G087600           GO:0016705
.
.
.
Run Code Online (Sandbox Code Playgroud)

我的input_file看起来像这样:

HORVU1Hr1G002090.11 HORVU1Hr1G002090    chr1H:4283580-4286133   HC_G    arginine decarboxylase 1    GO:0003824, GO:0006527, GO:0008295, GO:0008792  PF00278, PF02784    IPR000183, IPR002985, IPR009006, IPR022643, IPR022644, IPR022657, IPR029066 HORVU1Hr1G002090
HORVU1Hr1G005360.1  HORVU1Hr1G005360    chr1H:11579708-11582804 HC_G    Cytochrome P450 superfamily protein GO:0004497, GO:0005506, GO:0016705, GO:0020037, GO:0055114  PF00067 IPR001128, IPR002403, IPR017972    HORVU1Hr1G005360
HORVU1Hr1G087600.1  HORVU1Hr1G087600    chr1H:539679073-539680597   HC_G    Glutaredoxin family protein GO:0009055, GO:0015035, GO:0045454  PF00462 IPR002109, …
Run Code Online (Sandbox Code Playgroud)

awk grep sed string-matching

0
推荐指数
1
解决办法
111
查看次数

Python-替换字符串中除某些单词之外的所有单词

给定一个像这样的文本

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut ac massa arcu.
Run Code Online (Sandbox Code Playgroud)

我想编写一个函数,用给定的字符串xxxx替换文本中的所有单词(列表keep_list中定义的单词除外) 。因此,如果

keep_list = ['amet', 'ac']
Run Code Online (Sandbox Code Playgroud)

我想要一个像这样的输出

xxxx xxxx xxxx xxxx amet, xxxx xxxx xxxx. xxxx ac xxxx xxxx.
Run Code Online (Sandbox Code Playgroud)

我想保留原始字符串的所有逗号、点等,这是该函数的唯一困难。

你会如何以优雅的方式做到这一点?

python regex string string-matching

0
推荐指数
1
解决办法
1596
查看次数

-1
推荐指数
1
解决办法
2306
查看次数

标准化表示(组合)项链的字符串

我试图通过查找它们的线性表示来匹配Python中符号的"项链",我使用普通字符串.例如,字符串"AABC","ABCA","BCAA","CAAB"都表示相同的项链(如图).

符号循环

为了得到一个概述,我只存储一个作为"代表"给定的项链相当于字符串.至于检查我是否存储了候选项链,我需要一个函数来规范任何给定的字符串表示.作为一种伪代码,我在Python中编写了一个函数:

import collections

def normalized(s):
    q = collections.deque(s)
    l = list()
    l.append(''.join(q))
    for i in range(len(s)-1):
        q.rotate(1)
        l.append(''.join(q))
    l.sort()
    return l[0]
Run Code Online (Sandbox Code Playgroud)

对于上面示例项链中的所有字符串表示,此函数返回"AABC",首先按字母顺序排列.

由于我对Python比较陌生,我想 - 如果我开始在Python中实现一个应用程序 - 这个函数对于生产代码来说已经"足够好"吗?换句话说:有经验的Python程序员会使用这个函数,还是有明显的缺陷?

python combinatorics string-comparison string-matching

-1
推荐指数
1
解决办法
386
查看次数