LINQ replaces regex in all cases

Can a LINQ expression replace all cases where a regular expression has been used before?

In other words; Is there a regular expression that cannot be represented by a LINQ query?

+3
source share
5 answers

It is probably possible to create a LINQ expression for any given regular expression, but this will in many cases be unreasonable. Even if you eliminate things like backlinks, regular expressions can be arbitrarily complex. The beauty of regular expressions (and I find it somewhat unexpected that I use the term “beauty” to describe regular expressions) is that it is a compact and expressive, but very narrowly oriented tool for matching patterns in strings.

LINQ, on the other hand, is a very expressive general-purpose tool.

, (ab)+([0-9^%#@-.,]{1,5})ab[0-9]$. LINQ ? , - , , , , . , , , , .. , , .

, . . - , LINQ , .

+9

LINQ. Regex - , LINQ - . LINQ , . LINQ Regex. .

+4

" : , LINQ?"

.

var pattern = "anypattern";
string input = "someInput";
var q = input.Any(c => new Regex(pattern).IsMatch(input));

:)

+3

Fluent Regex? Fluent Regex :

Regex socialSecurityNumberCheck = new Regex(@"^\d{3}-?\d{2}-?\d{4}$");

:

Regex socialSecurityNumberCheck = new Regex(Pattern.With.AtBeginning
    .Digit.Repeat.Exactly(3)
    .Literal("-").Repeat.Optional
    .Digit.Repeat.Exactly(2)
    .Literal("-").Repeat.Optional
    .Digit.Repeat.Exactly(4)
    .AtEnd);

, !

+2

, , , RegEx, ? LINQ , . String, , , , RegEx .

0

All Articles