Mykhailo Mushynskyi
Mykhailo Mushynskyi

Reputation: 173

How to delete all symbols that don't match the pattern

I'm writing a type that stores building numbers. If the user tries to input a wrong string it should fit the string to specified pattern. My pattern is:[\d]+[a-zA-Z]?, so the string should look like 103a or 59. And if I have something like assdas103a4asdas1231as1 it should delete all except 103a.

Upvotes: 1

Views: 150

Answers (2)

ΩmegaMan
ΩmegaMan

Reputation: 31616

it should delete all except 103a.

Then only take the first match by calling Match such as:

var str = "assdas103a4asdas1231as1";

var buildingNumber = Regex.Match(str, @"[\d]+[a-zA-Z]?").Value;

Result put into buildingNumber is:

103a

Upvotes: 1

Elizabeth
Elizabeth

Reputation: 499

We can do this by finding the first match and storing it in a capture group, then replacing its text with the captured text.

^.*?(\d+[a-zA-Z]?).*

  • ^.*? matches as few characters as possible from the start of the line, meaning it will stop at our first match:

  • (\d+[a-zA-Z]?) is your existing regex, placed in a capture group.

  • .* continues until the end of the line.

Replacing this with the contents of group 1, \1, we turn the captured assdas 103a 4asdas1231as1 into just 103a.

Note that the anchor (^) isn't required, but they improve performance on a non-matching line because the engine won't attempt the match again on every starting position.

If you like, you could also just grab the first match and use its value instead of bothering to do it with a regex substitution. In that case you would not need the surrounding .* expressions.

Upvotes: 2

Related Questions