liveBook · Manning

Regular expression

As explained in Building Web APIs with ASP.NET Core

A regular expression is a formal pattern notation for matching strings that satisfy specified character and sequencing rules.

A regular expression is a formal pattern for matching character combinations in strings. In ASP.NET Core validation, the RegularExpression attribute applies such a pattern to incoming values. section 6.1.3

For a board-game API endpoint, the pattern-based validation flow is:

  • Place a RegularExpression attribute on the parameter that requires constrained text. section 6.1.3
  • Write a pattern such as ASC|DESC to match either permitted sort direction. section 6.1.3
  • Let the .NET regular expression engine use IsMatch internally to determine whether the input matches. section 6.1.3

Validating sort order

Annotating sortOrder with [RegularExpression("ASC|DESC")] accepts ASC or DESC while rejecting other values during request validation. section 6.1.3

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Defines patterns and shows attribute-based matching, but treats regular expressions only to a basic extent.
Examples
Uses ASC|DESC to validate a board-game API sortOrder parameter.
What you can do after
Apply it via the RegularExpression attribute to validate whether endpoint input matches an allowed pattern.
How it connects
Connects regular expressions with data validation, data annotations, ASP.NET Core, and the .NET regular expression engine.

Also explained in

Compare how 27 books explain regular expression →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.