其他教程

其他教程

Products

当前位置:首页 > 其他教程 >

在Go中通过正则表达式在字符串中查找字符串

GG网络技术分享 2025-03-18 16:15 1


问题描述:

I\'m trying to write a regex that returns a string to me

I have a full string like this:

@badges=moderator/1,premium/1;color=#00FF80;display-name=gempir;emotes=;id=d0358df2-f0a1-4600-910e-515ec52b1baa;mod=1;room-id=99659894;sent-ts=1489250823035;subscriber=0;tmi-sent-ts=1489250823211;turbo=0;user-id=77829817;user-type=mod :gempir!gempir@gempir.tmi.twitch.tv PRIVMSG #gempbot :!status

and what to find the command \"PRIVMSG\" because there are similar full strings that have different commands. Like this:

@ban-duration=20;ban-reason=Banned\\slink;room-id=11148817;target-user-id=78424343 :tmi.twitch.tv CLEARCHAT

If you are interested these are twitch irc messages with ircv3 tags

now I have this regex:

^@(.*)\\s:(.*?)tmi.twitch.tv\\s

how do I use the regex or a similar one in Go to find the string im looking for?

I wanted to avoid splitting again and again because it seems kinda unreasonable and hard to read. I think a regex can fix all my problems.

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

我正在尝试编写一个向我返回字符串的正则表达式</ p>

我有这样一个完整的字符串:</ p>

  @ badges =主持人/ 1,高级/ 1; color =#00FF80; display-name = gempir; emotes =; id =  d0358df2-f0a1-4600-910e-515ec52b1baa; MOD = 1;房间-ID = 99659894;发送-TS = 1489250823035;订户= 0; TMI-发送-TS = 1489250823211;涡轮= 0;用户ID = 77829817;用户 type = mod:gempir!gempir@gempir.tmi.twitch.tv PRIVMSG #gempbot:!status 

</ code> </ pre>

以及查找命令“ PRIVMSG”的内容,因为存在 具有不同命令的类似完整字符串。 像这样:</ p>

  @ ban-duration = 20; ban-reason = Banned \\ slink; room-id = 11148817; target-user-id = 78424343:tmi.twitch。 电视CLEARCHAT 

</ code> </ pre>

如果您有兴趣,这些是带有ircv3标签的抽搐irc消息</ p>

现在我有此正则表达式:</ p>

 <代码> ^ @(。*)\\ S:(?*)tmi.twitch.tv \\ S 

</代码> </ PRE>

我该如何在Go中使用正则表达式或类似的正则表达式来查找我正在寻找的字符串?</ p>

我想避免一次又一次地拆分,因为这似乎有点不合理且难以理解。 我认为正则表达式可以解决我所有的问题。</ p>

</ div>

网友观点:

The following regex should do it ...

(?<=twitch.tv\\s)\\w+(?=\\s#)

see regex demo

Just not to be anchored to the web-address (which can be changed some time) i\'d prefer this kind:

([A-Z]+)(\\s#.+)?$

GO字符串

字符串

在GO语言中,字符串是不可变的字节序列,只能通过重新赋值改变变量的值。

如下代码:

标签:

提交需求或反馈

Demand feedback