第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

識(shí)別推文消息中正確的標(biāo)簽索引

識(shí)別推文消息中正確的標(biāo)簽索引

Go
智慧大石 2022-11-15 10:47:05
我需要在 Twitter 消息(各種語言、表情符號(hào)等)中識(shí)別正確的索引。我找不到返回這些位置的解決方案,如下例所示。import (    "regexp"    "testing"    "github.com/stretchr/testify/require")func TestA(t *testing.T) {    text := "???? [URGENT] Les forces de dissuasion #nucleaire de la #Russie"    var re = regexp.MustCompile(`#([_A-Za-z0-9]+)`)    pos := re.FindAllStringIndex(text, -1)    // FindAllStringIndex returns    // [0][43,53]    // [1][60,67]    // These are the expected positions.    require.Equal(t, pos[0][0], 37)     require.Equal(t, pos[0][1], 47)    require.Equal(t, pos[1][0], 54)    require.Equal(t, pos[1][1], 61)}
查看完整描述

1 回答

?
MMTTMM

TA貢獻(xiàn)1869條經(jīng)驗(yàn) 獲得超4個(gè)贊

該FindAllStringIndex()函數(shù)返回字節(jié)的位置,而不是符文。


您需要import "unicode/utf8"并使用utf8.RuneCountInString(text[:pos[0][0]])等等,而不是pos[0][0]確保您計(jì)算 Unicode 代碼點(diǎn)而不僅僅是字節(jié):


// You can edit this code!

// Click here and start typing.

package main


import (

    "regexp"

    "testing"

    "unicode/utf8"


    "github.com/stretchr/testify/require"

)


func TestA(t *testing.T) {

    text := "???? [URGENT] Les forces de dissuasion #nucleaire de la #Russie"


    var re = regexp.MustCompile(`#\w+`)


    pos := re.FindAllStringIndex(text, -1)


    require.Equal(t, utf8.RuneCountInString(text[:pos[0][0]]), 37)

    require.Equal(t, utf8.RuneCountInString(text[:pos[0][1]]), 47)

    require.Equal(t, utf8.RuneCountInString(text[:pos[1][0]]), 54)

    require.Equal(t, utf8.RuneCountInString(text[:pos[1][1]]), 61)


}

請(qǐng)參閱Go 演示。


此外,#\w+是一個(gè)較短的模式來匹配 a #,然后是一個(gè)或多個(gè)字母、數(shù)字或下劃線。


查看完整回答
反對(duì) 回復(fù) 2022-11-15
  • 1 回答
  • 0 關(guān)注
  • 105 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)