获取以 5555/ 开头、以 0/ 结尾的字符串的最后一个单词
我有一个包含由套接字发送的双数据的字符串。由于网络延迟,我在客户端获得过载的数据,这意味着我的实际字符串是
5555/57.6626/63.364/0/
我在客户端得到这个字符串:
5555/989.994/262.65645/0/5555/165.6515/6526.545/0/
所以基本上两个字符串被合并了。我想要最后更新的字符串以粗体格式显示。
请注意, 5555/
和 /0/
是分隔符,我的实际数据位于这些分隔符之间。
I have a string containg double data that is being sent by sockets. Due to network delay I get overloaded data on the client side, meaning my actual string is
5555/57.6626/63.364/0/
and I get this string on client side:
5555/989.994/262.65645/0/5555/165.6515/6526.545/0/
So basically two strings are merged. I want the last updated string that is in bold format.
Note that 5555/
and /0/
are the delimiters, my actual data is between these delimiters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
结果:
result:
另一种变体
C# 8.0
another variant
C# 8.0
为了得到最后一次出现的情况,并且考虑到
您可以使用匹配
5555/
的模式,然后使用负前瞻( ?!\S*/5555/)
然后匹配直到
/0/
。由于示例字符串中没有空格,因此您可以使用\S*
来匹配可选的非空白字符。输出
To get the last occurrence, and given that
you can use a pattern matching
5555/
and then assert no more occurrences of that part to the right using a negative lookahead(?!\S*/5555/)
Then match until
/0/
. As there are no spaces in the example string, you can use\S*
to match optional non whitespace characters.Output
您可以使用正则表达式:
解释:
(?<=(^|/)5555/)
将在输入开头或“/”之后检查“5555/”,但是不将其包含在匹配中[\d\./]+?
将匹配任何数字、点和斜杠序列(?
用于非贪婪匹配,即可能的最短匹配)(?=/0(/|$))
将检查输入末尾或“/”之前的“/0”,但不将其包含在匹配中这将产生两个匹配“989.994/262.65645”和“165.6515/6526.545”;就拿最后一个吧。
You could use a regular expression:
Explanation:
(?<=(^|/)5555/)
will check for "5555/" at the beginning of the input or after a "/", but not include this in the match[\d\./]+?
will match any sequence of digits, dots and slashes (the?
is for a non-greedy match, i.e. the shortest match possible)(?=/0(/|$))
will check for "/0" at the end of the input or preceding a "/", but not include this in the matchThis will produce two matches "989.994/262.65645" and "165.6515/6526.545"; just take the last one.