首页 > 자바 > 본문

使用正则表达式查找具有相似性的文本

王林
发布: 2024-02-14 19:03:08
转载
673명이 탐색했습니다.

php小编柚子正则表达式是一种强大的文本匹配工具,能够帮助我们快速查找具有相似性的文本。无论是在字符串处理、数据提取还是验证输入等方面,正则表达式都发挥着重要作用。它的灵活性和高效性使得我们能够更加方便地处理复杂的文本操作,大大提高了开发效率。无论是初学者还是有经验的开发者,掌握正则表达式都是一项必备技能,让我们一起来探索它的魅力吧!

问题内容

我识别了不同 pdf 文档中的文本列表。现在我需要使用正则表达式从每个文本中提取一些值。我的一些模式是这样的:

some text[ -]?(.+)[ ,-]+some other text
登录后复制

但问题是,识别后有些字母可能会出错("0" 代替 "o""i" 代替 "l" 等)。这就是为什么我的模式与它不匹配。

我想使用类似 jaro-winkler 或 levenshtein 相似性的正则表达式,这样我就可以从 s0me 文本 my_value、一些其他文本 等文本中提取 my_value

我知道这看起来棒极了。但也许这个问题有解决方案。

顺便说一句,我正在使用 java,但可以接受其他语言的解决方案

解决方法

如果在python中使用regex模块,则可以使用模糊匹配。以下正则表达式允许每个短语最多出现 2 个错误。您可以使用更复杂的错误测试(用于插入、替换和删除),有关详细信息,请参阅链接文档。

import regex

txt = 's0me text my_value, some otner text'
pattern = regex.compile(r'(?:some text){e<=2}[ -]?(.+?)[ ,-]+(?:some other text){e<=2}')

m = pattern.search(txt)
if m is not none:
    print(m.group(1))
登录后复制

输出:

my_value
登录后复制
package main

import (
    "fmt"
    "regexp"
    "strings"

    "github.com/agnivade/levenshtein"
)

func findClosestMatch(text string, candidates []string, threshold int) (string, bool) {
    for _, candidate := range candidates {
        if levenshtein.ComputeDistance(strings.ToLower(text), strings.ToLower(candidate)) <= threshold {
            return candidate, true
        }
    }
    return "", false
}

func findMatches(text string, threshold int) []string {
    // Broad regex to capture potential matches
    re := regexp.MustCompile(`(?i)(some\s*\w*\s*text\s*)([^,]+)`)
    potentialMatches := re.FindAllStringSubmatch(text, -1)

    var validMatches []string
    expectedPattern := "some text" // The pattern we expect to find

    for _, match := range potentialMatches {
        // Check if the first part of the match is close to our expected pattern
        closestMatch, isClose := findClosestMatch(match[1], []string{expectedPattern}, threshold)
        if isClose {
            // If the first part is close to 'some text', add the second part to valid matches
            validMatches = append(validMatches, strings.TrimSpace(closestMatch))
        }
    }

    return validMatches
}

func main() {
    text := "This is a sample text with s0me text MY_VALUE, some otner text."
    threshold := 10 

    matches := findMatches(text, threshold)
    fmt.Println("Matches found:", matches)
}
登录后复制

正则表达式模式 (?i)(some\s*\w*\s*text\s*)([^,]+) 用于捕获类似于“some text”的短语,后跟逗号之前的任何字符

위 내용은 使用正则表达式查找具有相似性的文本의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

来源:stackoverflow.com
본 웹사이트의 성명
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
더>
最新下载
더>
网站特效
网站源码
网站素材
프론트엔드 템플릿
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!