python 字符串匹配like_Python:找到完全匹配(Python: find exact match)

Python:找到完全匹配(Python: find exact match)

我想使用find()找到完全匹配的单词。 但是它的接缝find()也返回部分匹配的字符串。

参考stackoverflow中的先前帖子字符串完全匹配使用以下正则表达式:

import re

print(re.findall('\\blocal\\b', "Hello, locally local test local."))

// ['local', 'local']

问题在于我的情况是“本地的”。 与“本地”不同

我怎样才能做到这一点?

更新:

我需要做的是替换包含本地工作的元素。

print("Hello, locally local test local#".replace('local','we'))

// should result in

Hello, locally we test local#

I would like to find words with the exact match using the find(). But it seams that find() returns strings with partial match as well.

Referring to previous posts in stackoverflow String exact match the following regex was used:

import re

print(re.findall('\\blocal\\b', "Hello, locally local test local."))

// ['local', 'local']

The problem is that in my case "local." is different from "local"

How can I do that?

update:

What I need to do is to the replace the element which contains the work local.

print("Hello, locally local test local#".replace('local','we'))

// should result in

Hello, locally we test local#

原文:https://stackoverflow.com/questions/13070566

更新时间:2020-10-13 16:10

最满意答案

如果你要返回的所有内容都是你要查找的内容的列表,那么找不到它...对于这个用例,我不打扰re,只需使用:

'Hello, locally local test local.'.split().count('local')

好的,有关的更新:

我需要做的是替换包含本地工作的元素。

我会去做类似的事情:

re.sub(r'\blocal([\b\s])', r'we\1', s)

It doesn't make sense for a findall if all you're returning is a list of what you're looking for... For this use case, I wouldn't bother with an re, and just use:

'Hello, locally local test local.'.split().count('local')

Okay, an update related to:

What I need to do is to the replace the element which contains the work local.

I'd go for something like:

re.sub(r'\blocal([\b\s])', r'we\1', s)

2012-10-25

相关问答

你拥有的代码应该可以正常工作。 你是如何检查结果的? 如果未找到匹配项, array.indexOf('leo')将返回-1 。 The code you have should work just fine. How are you checking the result? array.indexOf('leo') will return -1 if no match is found.

您可以使用XPath找到特定的entry元素。 import xml.etree.ElementTree as ET

tree = ET.parse("sample.xml")

# Find the element that has a 'key' attribute with a value of 'applications'

entry = tree.find(".//entry[@key='applications']")

# Change the value of the 'v

...

使用字边界如: s/\b1\b/2/g foreach @remaining;

要同时替换0和1: s/\b([01])\b/1+$1/eg foreach @remaining;

Use word boundaries like: s/\b1\b/2/g foreach @remaining;

To replace 0 and 1 at the same time: s/\b([01])\b/1+$1/eg foreach @remaining;

使用正则表达式匹配 $title = "Music award";

if (preg_match('/\bwar\b/', $title) !== false) {

echo "found";

}

Use regex matching $title = "Music award";

if (preg_match('/\bwar\b/', $title) !== false) {

echo "found";

}

您正在寻找的是一个单独的字符串,因此您可能希望它通过空格与其余字符串分开(这是正则表达式中的技术术语;请参见下文)。 我会尝试使用grep -o 'http://[a-zA-Z.-]*/[[:space:]]' text.log这匹配单词末尾的任何空格,还有制表符和换行符。 此外,某些URL可能包含数字或下划线,因此您可能需要进一步调整模式。 要包含这两个grep -o 'http://[0-9a-zA-Z.-_]*/[[:space:]]' text.log What you are looki

...

您将错误的参数传递给Regex.Match方法。 你需要像这样交换参数> Match match = Regex.Match(myword,pattern);

You're passing wrong arguments to Regex.Match method. You need to swap arguments like this> Match match = Regex.Match(myword,pattern);

我认为正则表达式可能在这里很好用: where num_text ~ '\D10/15\D|^10/15\D|\D10/15$|^10/15$'

\D atom表示任何非数字。 这有点模糊,但ts_vector可能实际上适合你: select *

from Your_Table

where

to_tsvector(num_text) @@ '10/15'

ts_vector方法作为它的可扩展性优势 - 如果你正在搜索数百万行,有很多方法可以让它变得非常快。 如果你有1000行,那么它几乎不

...

如果你要返回的所有内容都是你要查找的内容的列表,那么找不到它...对于这个用例,我不打扰re,只需使用: 'Hello, locally local test local.'.split().count('local')

好的,有关的更新: 我需要做的是替换包含本地工作的元素。 我会去做类似的事情: re.sub(r'\blocal([\b\s])', r'we\1', s)

It doesn't make sense for a findall if all you're returning

...

您的第二个查询对您要检查的用户一无所知。 这几乎允许用户以任何人身份登录,如果他们知道数据库中至少有1个密码。 $result = $conn->query("SELECT password FROM Users WHERE password='" . $_POST["pwd"] . "' AND username = '...'");

Your second query knows nothing about which user you're checking against. This p

...

更新:有可能,请参阅@ pguardiario的答案 原始答案。 (至少另一种选择): 不,不幸的是用phpQuery是不可能的。 但是可以使用XPath轻松完成。 想象一下你必须遵循HTML: $html = <<test

HelloHello World

...

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值