Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Regex Escape Sequences

Tags:

c#

regex

Is there a complete list of regex escape sequences somewhere? I found this, but it was missing \\ and \e for starters. Thus far I have come up with this regex pattern that hopefully matches all the escape sequences:

 @"\\([bBdDfnreasStvwWnAZG\\]|x[A-Z0-9]{2}|u[A-Z0-9]{4}|\d{1,3}|k<\w+>)"
like image 356
mpen Avatar asked Nov 25 '10 10:11

mpen


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.


3 Answers

Alternatively, if you only want to escape a string correctly, you could just depend on Regex.Escape() which will do the necessary escaping for you.

Hint: There is also a Regex.Unescape()

like image 53
VVS Avatar answered Oct 12 '22 21:10

VVS


This MSDN page (Regular Expression Language Elements) is a good starting place, with this subpage specifically about escape sequences.

like image 40
Jon Skeet Avatar answered Oct 12 '22 21:10

Jon Skeet


Don't forget the zillions of possible unicode categories: \p{Lu}, \P{Sm} etc.

There are too many of these for you to match individually, but I suppose you could use something along the lines of \\[pP]\{[A-Za-z0-9 \-_]+?\} (untested).

And there's also the simpler stuff that's missing from your list: \., \+, \*, \? etc etc.

If you're simply trying to unescape an existing regex then you could try Regex.Unescape. It's not perfect, but it's probably better than anything you or I could knock up in a short space of time.

like image 29
LukeH Avatar answered Oct 12 '22 21:10

LukeH