获取以 5555/ 开头、以 0/ 结尾的字符串的最后一个单词

发布于 2025-01-15 02:09:57 字数 354 浏览 3 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

濫情▎り 2025-01-22 02:09:57
var s = "5555/989.994/262.65645/0/5555/165.6515/6526.545/0/";
var x = s.Substring(s.LastIndexOf("5555/") + 5).
    Substring(0, s.LastIndexOf("0/") - s.LastIndexOf("5555") - 5);

结果:

165.6515/6526.545/
var s = "5555/989.994/262.65645/0/5555/165.6515/6526.545/0/";
var x = s.Substring(s.LastIndexOf("5555/") + 5).
    Substring(0, s.LastIndexOf("0/") - s.LastIndexOf("5555") - 5);

result:

165.6515/6526.545/
瑕疵 2025-01-22 02:09:57

另一种变体

var s = "5555/989.994/262.65645/0/5555/165.6515/6526.545/0/";
var x = s.Split("5555/").Last().Split("0/")[0];

C# 8.0

var x = s.Split("5555/")[^1].Split("0/")[0];

another variant

var s = "5555/989.994/262.65645/0/5555/165.6515/6526.545/0/";
var x = s.Split("5555/").Last().Split("0/")[0];

C# 8.0

var x = s.Split("5555/")[^1].Split("0/")[0];
浪推晚风 2025-01-22 02:09:57

为了得到最后一次出现的情况,并且考虑到

/5555 和 /0/ 之间的数据不可能是 5555 和 0

您可以使用匹配 5555/ 的模式,然后使用负前瞻 ( ?!\S*/5555/)

然后匹配直到/0/。由于示例字符串中没有空格,因此您可以使用 \S* 来匹配可选的非空白字符。

var input = "5555/989.994/262.65645/0/5555/165.6515/6526.545/0/";
var m = Regex.Match(input, @"5555/(?!\S*/5555/)\S*/0/");
if (m.Success) {
    Console.WriteLine(m.Value);
}

输出

5555/165.6515/6526.545/0/

To get the last occurrence, and given that

data between /5555 and /0/ can not ever be 5555 and 0

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.

var input = "5555/989.994/262.65645/0/5555/165.6515/6526.545/0/";
var m = Regex.Match(input, @"5555/(?!\S*/5555/)\S*/0/");
if (m.Success) {
    Console.WriteLine(m.Value);
}

Output

5555/165.6515/6526.545/0/
梦情居士 2025-01-22 02:09:57

您可以使用正则表达式:

Regex.Matches(input, @"(?<=(^|/)5555/)[\d\./]+?(?=/0(/|$))")

解释:

  • (?<=(^|/)5555/) 将在输入开头或“/”之后检查“5555/”,但是不将其包含在匹配中
  • [\d\./]+? 将匹配任何数字、点和斜杠序列(? 用于非贪婪匹配,即可能的最短匹配)
  • (?=/0(/|$)) 将检查输入末尾或“/”之前的“/0”,但不将其包含在匹配中

这将产生两个匹配“989.994/262.65645”和“165.6515/6526.545”;就拿最后一个吧。

You could use a regular expression:

Regex.Matches(input, @"(?<=(^|/)5555/)[\d\./]+?(?=/0(/|$))")

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 match

This will produce two matches "989.994/262.65645" and "165.6515/6526.545"; just take the last one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文