我需要在给定正则表达式的字符串中找到所有匹配项.我一直在findall()这样做,直到我遇到一个没有按照我的预期做的情况.例如:
regex = re.compile('(\d+,?)+')
s = 'There are 9,000,000 bicycles in Beijing.'
print re.search(regex, s).group(0)
> 9,000,000
print re.findall(regex, s)
> ['000']
Run Code Online (Sandbox Code Playgroud)
在这种情况下search()返回我需要的(最长匹配)但findall()行为不同,虽然文档暗示它应该是相同的:
findall()匹配所有出现的模式,而不仅仅是第一个模式search().
为什么行为不同?
我怎样才能实现的结果search()与findall() (或别的东西)?
我有包含逗号分隔值的字段.我需要提取列表中的最后一个元素.我试过这个:
select list_field, LTRIM(RTRIM(right(list_field, len(list_field) - CHARINDEX(',',list_field))))
Run Code Online (Sandbox Code Playgroud)
但它会在第一个逗号出现后返回列表的最后一部分.例如,
a,b返回b
a,b,c返回b,c
我想使用像模式一样的正则表达式.在TSQL(sql server 2008)中是否可能?还有其他线索吗?
我正在研究字符串匹配算法.我遇到的最有用的是我的手机使用的一个(SE xPeria neo v上的android 2.3.4).

如截图中所示,我按下了我想要的字符附近的字符jiw,并且它正确显示.
似乎算法类似于levenstein距离(我的输入和字典之间的距离).不知何故,近字符在字符串匹配中有一些价值.
有关正在使用的算法的任何想法?
我正在尝试创建一个新闻文章数据框的子集,其中至少提到一组关键字或短语的一个元素.
# Sample data frame of articles
articles <- data.frame(id=c(1, 2, 3, 4), text=c("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod", "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,", "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo", "consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse"))
articles$text <- as.character(articles$text)
# Sample vector of keywords or phrases
keywords <- as.character(c("elit", "tempor incididunt", "reprehenderit"))
# id text
# 1 …Run Code Online (Sandbox Code Playgroud) 我需要以这种方式匹配我的字符串:*myString*
where *表示任何子字符串.我应该使用哪种方法?
你能帮我吗?
我比较歌曲名称,用拉丁文字(但并不总是),我的目标是一种算法,给出了一个高分,如果两页首歌的标题似乎是相同的同题和非常低的分数,如果他们没有任何共同之处.
现在我已经不得不使用Lucene和RAMDirectory来编写代码(Java) - 但是使用Lucene只是为了比较两个字符串太重,因此太慢了.我现在转向使用https://github.com/nickmancol/simmetrics,它有许多很好的算法来比较两个字符串:
BlockDistance
ChapmanLengthDeviation
ChapmanMatchingSoundex
ChapmanMeanLength
ChapmanOrderedNameCompoundSimilarity
CosineSimilarity
DiceSimilarity
EuclideanDistance
InterfaceStringMetric
JaccardSimilarity
Jaro
JaroWinkler
Levenshtein
MatchingCoefficient
MongeElkan
NeedlemanWunch
OverlapCoefficient
QGramsDistance
SmithWaterman
SmithWatermanGotoh
SmithWatermanGotohWindowedAffine
Soundex
Run Code Online (Sandbox Code Playgroud)
但是我不熟悉这些算法,那将是一个不错的选择?
我认为Lucene以某种形式使用CosineSimilarity,所以这是我的出发点,但我认为可能有更好的东西.
具体来说,算法应该在短字符串上工作,并且应该理解单词的概念,即应该特别对待空格.拉丁文字的良好匹配是最重要的,但是韩文和中文等其他文字的良好匹配也是相关的,但我希望因为它们对待空间的方式需要不同的算法.
我用c ++实现了Damerau-Levenshtein距离,但它没有为输入提供正确的o/p(pantera,aorta)正确的o/p是4但是我的代码给出了5 .....
int editdist(string s,string t,int n,int m)
{
int d1,d2,d3,cost;
int i,j;
for(i=0;i<=n;i++)
{
for(j=0;j<=m;j++)
{
if(s[i+1]==t[j+1])
cost=0;
else
cost=1;
d1=d[i][j+1]+1;
d2=d[i+1][j]+1;
d3=d[i][j]+cost;
d[i+1][j+1]=minimum(d1,d2,d3);
if(i>0 && j>0 && s[i+1]==t[j] && s[i]==t[j+1] ) //transposition
{
d[i+1][j+1]=min(d[i+1][j+1],d[i-1][j-1]+cost);
}
}
}
return d[n+1][m+1];
}
Run Code Online (Sandbox Code Playgroud)
我没有看到任何错误.有人可以发现代码有问题吗?
我有一个多行字符串,如下所示:
2012-15-08 07:04 Bla bla bla blup
2012-15-08 07:05 *** Error importing row no. 5: The import of this line failed because bla bla
2012-15-08 07:05 Another text that I don't want to search...
2012-15-08 07:06 Another text that I don't want to search...
2012-15-08 07:06 *** Error importing row no. 5: The import of this line failed because bla bla
2012-15-08 07:07 Import has finished bla bla
Run Code Online (Sandbox Code Playgroud)
我想要的是在RegularExpression(使用PowerShell)的帮助下提取所有有错误的行号.所以我需要找到"***Error import row no."和下面的":"之间的数字,因为这总是会给我行号.
我查看了其他各种RegEx问题,但说实话,答案对我来说就像中文.
试图在http://regexr.com/的帮助下构建RegEx 但到目前为止还没有成功,例如使用以下模式:
"Error importing row …Run Code Online (Sandbox Code Playgroud) 我有2个向量.
x=c("a", "b", "c", "d", "a", "b", "c")
y=structure(c(1, 2, 3, 4, 5, 6, 7, 8), .Names = c("a", "e", "b",
"c", "d", "a", "b", "c"))
Run Code Online (Sandbox Code Playgroud)
我想匹配a到a,b到b相应的序列,从而使x[2]匹配y[3],而不是y[7]; 和x[5]匹配y[6]而不是y[1]等等.
lapply(x, function(z) grep(z, names(y), fixed=T))
Run Code Online (Sandbox Code Playgroud)
得到:
[[1]]
[1] 1 6
[[2]]
[1] 3 7
[[3]]
[1] 4 8
[[4]]
[1] 5
[[5]]
[1] 1 6
[[6]]
[1] 3 7
[[7]]
[1] 4 …Run Code Online (Sandbox Code Playgroud) string-matching ×10
string ×3
algorithm ×2
r ×2
regex ×2
c++ ×1
findall ×1
grep ×1
grepl ×1
iphone ×1
java ×1
lucene ×1
objective-c ×1
powershell ×1
python ×1
search ×1
similarity ×1
suffix-tree ×1
t-sql ×1
vector ×1