Domnic
Domnic

Reputation: 3867

Regex pattern in c# start with @ and end with 9;

Need regex pattern that text start with"@" and end with " ";

I tried the below pattern

string pattern = "^[@].*?[	]$";

but not working

Upvotes: 0

Views: 2052

Answers (5)

STORM
STORM

Reputation: 4331

This patterns works fine. I have tested it.

string pattern = "#(.*?)9";

See below link to test it online.

https://regex101.com/r/iR6nP6/1

C#

const string str = "dadasd#beetween9ddasdasd";
var match = Regex.Match(str, "#(.*?)9");
Console.WriteLine(match.Groups[1].Value);

Upvotes: 1

tchelidze
tchelidze

Reputation: 8308

You should use \t to match tab character

You can use special character sequences to put non-printable characters in your regular expression. Use \t to match a tab character (ASCII 0x09)

Try following Regex

^\@.*\t\;$

Upvotes: 0

counterflux
counterflux

Reputation: 383

you mean something like:

string pattern = "^@.*?[ ]$"

There are also many fine regex expression helpers on the web. for example https://regex101.com/ It gives a nice explanation of how your text will be handled.

Upvotes: 0

Soner Gönül
Soner Gönül

Reputation: 98750

Since 	 is an hex code of tab character, why not just using StartsWith and EndsWith methods instead?

if(yourString.StartsWith("@") && yourString.EndsWith("\\t"))
{
    // Pass
}

Upvotes: 3

npinti
npinti

Reputation: 52185

In regex syntaxt, the [] denotes a group of characters of which the engine will attempt to match one of. Thus, [&#x9] means, match one of an &, #, x or 9 in no particular order.

If you are after order, which seems you are, you will need to remove the []. Something like so should work: string pattern = "^@.*?&#x9$";

Upvotes: 0

Related Questions