从字符串中提取文本、连字符和数字

发布于 2024-12-23 17:00:37 字数 443 浏览 0 评论 0原文

在工作中,我们使用非常非常古老的全球分销系统进行预订,当我们取消预订时,我们会收到他们的网络服务的响应,说明预订是否已成功取消。

响应包含一个布尔值来表示它是否被取消,以及一个包含任何其他信息的字符串,例如取消引用或为什么无法取消等等。

如果成功取消,取消引用将被绑在中间响应中的字符串,看起来像这样

"NXT REPLACES  1  REDISPLAY ITINERARY1CXL-13113654 THANK YOU FOR YOUR INTEREST"

从这个字符串中我需要提取“CXL-13113654”...

基本上CXL后跟“-”然后是任何字符,但不包括“”

我已经搜索了谷歌大学和我能找到的一切似乎只是提取数字、字符或符号,而不是像我这样的设定格式的混合。

有人可以帮忙吗?

这怎么能做到呢?

In work we use a Global Distribution System for bookings which is very very old, when we cancel a booking we get a response from their web service to say if the booking has been successfully cancelled.

The response contains a bool to say if it is cancelled or not and a string with any other info such as cancellation reference or why it could not be cancelled etc etc.

If it is successfully cancellled the cancellation reference is tied up in the middle of the string within the response and looks something like this

"NXT REPLACES  1  REDISPLAY ITINERARY1CXL-13113654 THANK YOU FOR YOUR INTEREST"

From this string I need to extract "CXL-13113654"...

Basically the CXL followed by the "-" then any character upto but not including the " "

I have searched university of google and everything I can find seems to be only extracting numbers, characters or symbols never a mixture within a set format like mine.

Can anyone help?

How can this be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

泪意 2024-12-30 17:00:37

正则表达式模式:

System.Text.RegularExpressions.Regex.Match(inputString, @"(?<match>CXL\-[^\s]+)").Groups["match"].Value

Regex pattern:

System.Text.RegularExpressions.Regex.Match(inputString, @"(?<match>CXL\-[^\s]+)").Groups["match"].Value
秋意浓 2024-12-30 17:00:37

假设它是单行搜索...您需要一个像这样的正则表达式...

(?<Anything>[^CXL[\s]*\-[\s]*[^\s]+)

这会查找 CXL 后跟任意数量的空格(0 或更多),然后是连字符,然后是 0 或更多空格,然后然后匹配所有非空白。所有这些都将被放入名为“Anything”的组中。 您可以在此页面上进行测试,如果您就像

那么用于此的 C# 将是...

// -- in using statements add this
using System.Text.RegularExpressions;

// -- in your code add something like this

var inputString = "NXT REPLACES  1  REDISPLAY ITINERARY1CXL-13113654 THANK YOU FOR YOUR INTEREST";
var match = Regex.Match(inputString, @"(?<Anything>CXL[\s]*\-[\s]*[^\s]+) ");
if(match.success && match.Groups["Anything"].Success)
{
  var anything = match.Groups["Anything"].Value;
  // -- do something with anything
}

Assuming it's a single line search... you'll want a Regex like this....

(?<Anything>[^CXL[\s]*\-[\s]*[^\s]+)

This looks for CXL followed by white-space of any amount (0 or more), then a hyphen, then 0 or more whitespace, and then match all non whitespace. All of this will be put into the group called "Anything". You can test it on this page if you like.

The C# for this would then be...

// -- in using statements add this
using System.Text.RegularExpressions;

// -- in your code add something like this

var inputString = "NXT REPLACES  1  REDISPLAY ITINERARY1CXL-13113654 THANK YOU FOR YOUR INTEREST";
var match = Regex.Match(inputString, @"(?<Anything>CXL[\s]*\-[\s]*[^\s]+) ");
if(match.success && match.Groups["Anything"].Success)
{
  var anything = match.Groups["Anything"].Value;
  // -- do something with anything
}
╭⌒浅淡时光〆 2024-12-30 17:00:37
using System;
using System.Text.RegularExpressions;

class Program
{
    public static void Main()
    {
        var input = "NXT REPLACES 1 REDISPLAY ITINERARY1CXL-13113654 THANK YOU FOR YOUR INTEREST";
        var match = Regex.Match(input, @"CXL\-(?<number>\d+)\s+");
        if (match.Success)
        {
            Console.WriteLine(match.Groups["number"]);
        }
    }
}

印刷:

13113654
using System;
using System.Text.RegularExpressions;

class Program
{
    public static void Main()
    {
        var input = "NXT REPLACES 1 REDISPLAY ITINERARY1CXL-13113654 THANK YOU FOR YOUR INTEREST";
        var match = Regex.Match(input, @"CXL\-(?<number>\d+)\s+");
        if (match.Success)
        {
            Console.WriteLine(match.Groups["number"]);
        }
    }
}

prints:

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