目錄
問題內容
解決方法
首頁 後端開發 Golang python 正規表示式 split 函數在 golang 中等效

python 正規表示式 split 函數在 golang 中等效

Feb 13, 2024 pm 07:45 PM
字串數組

python 正则表达式 split 函数在 golang 中等效

在程式設計領域中,正規表示式是一種強大的工具,用於匹配和處理字串。在Python中,split函數是一個常用的正規表示式函數,用於將字串分割成子字串。然而,對於使用Golang的開發者來說,他們可能想知道在Golang中如何實現與Python中split函數相同的功能。在本文中,php小編子墨將向您介紹如何在Golang中等效地使用正規表示式split函數,以便更好地利用這個功能強大的工具。

問題內容

我有下面的 python 程式碼來匹配正規表示式::

import re
digits_re = re.compile("([0-9ee.+]*)")

p = digits_re.split("hello, where are you 1.1?")

print(p)
登入後複製

它給了這個輸出:: ['', '', 'h', 'e', '', '', 'l', '', 'l', '', 'o', '', ',', ' ', ' '、''、'w'、''、'h'、'e'、''、''、'r'、'e'、''、''、' '、''、' a' , '', 'r', 'e', '', '', ' ', '', 'y', '', 'o', '', 'u', '', ' ', '1.1 ', '', '', '?', '', '']

我正在嘗試使用 golang 來獲得上述輸出。

package main

import (
    "bytes"
    "fmt"
    "log"
    "os/exec"
    "regexp"
    "strconv"
)

func main() {
    // execute the command and get the output
    cmd := exec.command("echo", "hello, where are you 1.1?")
    var out bytes.buffer
    cmd.stdout = &out
    err := cmd.run()
    if err != nil {
        log.fatal(err)
    }

    // extract the numerical values from the output using a regular expression
    re := regexp.mustcompile(`([0-9ee.+]*)`)
    //matches := re.findallstring(out.string(), -1)
    splits := re.split(out.string(), -1)
    fmt.println(splits)

}
登入後複製

我得到如下輸出::

[H l l o ,   w h r   a r   y o u   ? 
]
登入後複製

我認為正規表示式與語言相關,因此 python 中使用的 split() 函數對 golang 沒有幫助。使用了 regexp 套件中的多個 find*() 函數,但找不到可以提供上述 python 程式輸出的函數。

輸出字串數組的目標是分隔無法轉換為 float 的字符,如果字串可以解析為浮點數,我會計算移動平均值。

最後,我將所有內容結合起來並呈現輸出,例如 linux watch 命令。

您需要更多詳細資訊/背景嗎?我很樂意分享。

非常感謝您的幫忙!

解決方法

同時,我得到了一些適合我的用例的東西。它與 python 的格式不完全相同,但沒有空字串/字元。

我使用 splitfindallstring 函數來獲得所需的輸出。

// unfortunately go regex split doesn't work like python
    // so we construct the entire array with matched string (numericals)
    // and the split strings to combine the output
    splits := digitsre.split(string(out), -1)
    matches := digitsre.findallstring(string(out), -1)

    p := []string{}
    for i := 0; i < len(splits); i++ {
        p = append(p, matches[i])
        p = append(p, splits[i])
    }
登入後複製

透過上面的程式碼片段,我得到類似 ::

[H e l l o ,   w h e r e   a r e   y o u  1.1 ?]
登入後複製

我不太擅長 regex,不知何故,上面的方法適合我的用例。

說得輕鬆一點,我聽到同事開玩笑說,如果你在 regex 的幫助下解決問題,你最終會遇到兩個問題! ! !

以上是python 正規表示式 split 函數在 golang 中等效的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

oracle中split()函數用法 oracle中split()函數用法 May 07, 2024 pm 01:06 PM

oracle中split()函數用法

java怎麼對字串排序 java怎麼對字串排序 Apr 02, 2024 am 02:18 AM

java怎麼對字串排序

\0在c語言中是什麼意思 \0在c語言中是什麼意思 Apr 27, 2024 pm 10:54 PM

\0在c語言中是什麼意思

java中的args是什麼意思 java中的args是什麼意思 May 07, 2024 am 02:24 AM

java中的args是什麼意思

args在java中是什麼意思 args在java中是什麼意思 Apr 25, 2024 pm 10:15 PM

args在java中是什麼意思

在C語言環境下如何對中文字元進行排序? 在C語言環境下如何對中文字元進行排序? Feb 18, 2024 pm 02:10 PM

在C語言環境下如何對中文字元進行排序?

C++ 函式對程式效能有哪些影響? C++ 函式對程式效能有哪些影響? Apr 12, 2024 am 09:39 AM

C++ 函式對程式效能有哪些影響?

C語言程式的啟動點是哪裡? C語言程式的啟動點是哪裡? Feb 20, 2024 pm 12:12 PM

C語言程式的啟動點是哪裡?

See all articles