DotNet equivalent to Java Matcher.hitEnd()

I am trying to set up a series of regular expressions to match unknown incoming data.

What I want to be able to to do is tell whether each expression matches, can never match, or might match the data so far.

For example, if we have the following regular expressions:

  • Aaaab
  • Aaaac
  • Abbb

Then if we have so far received aa then the first two might match, depending on what comes next, but the third will never match.

If we receive some more and get aaac then the first won’t match, the second does match, and the third still won’t match.

In Java this can be done like so:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Pattern pattern = Pattern.compile("Aaaab");
Matcher matcher = pattern.matcher(receivedSoFar);
if (matcher.matches())
{
// A match has been found now
}
else if (matcher.hitEnd())
{
// Doesn't match yet, but don't write it off, wait for more data.
}
else
{
// This pattern doesn't match at all, we can rule it out.
}
</code>
<code>Pattern pattern = Pattern.compile("Aaaab"); Matcher matcher = pattern.matcher(receivedSoFar); if (matcher.matches()) { // A match has been found now } else if (matcher.hitEnd()) { // Doesn't match yet, but don't write it off, wait for more data. } else { // This pattern doesn't match at all, we can rule it out. } </code>
Pattern pattern = Pattern.compile("Aaaab");
Matcher matcher = pattern.matcher(receivedSoFar);
if (matcher.matches())
{
  // A match has been found now
}
else if (matcher.hitEnd())
{
  // Doesn't match yet, but don't write it off, wait for more data.
}
else
{
  // This pattern doesn't match at all, we can rule it out.
}

See here for a working example.

The distinction between “might match” and “doesn’t match” is important because if we reach the point where none of the patterns can possibly match then we need to trigger an error state response.

I have been looking for a way to implement this in C# DotNet and I haven’t been able to find one. The System.Text.RegularExpressions.Match class only has a Success property which tells us whether the match has been found but can’t tell us whether a future match is possible.

Is there any way to do this in C# DotNet?

I have tried google, the learn.microsoft.com documentation and even, out of desperation, ChatGPT. None came up with any suggestions.

Edited: Sorry, I accidentally referenced requireEnd where I meant hitEnd.

8

Have a look at https://github.com/ltrzesniewski/pcre-net (Perl Compatible Regular Expressions for .NET)

There is a section about partial matching in the readme, it might cover your use case.

I don’t know a good way to solve the requirement with the .Net internal Regex implementation, so using an alternative implementation nuget might be your best bet.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật