Expresión regex

I need regular expression (C#) for symbol * - it should match any number of any characters, but it can contain only one space. I tried following, but its not working:


any way how to create regex like this?

Example: If user write expression MTN*-* it has to match for example


pero no

MTN311100  MTN3111-0000

preguntado el 09 de marzo de 12 a las 14:03

could you make an example of a passing and failing input? -

[^\s] matches the character not in the specified set of characters. -

You say it is for the character -, but then mention one space. However you matching example does not have any spaces. I'm not sure I understand what you need. -

3 Respuestas

You could use this expression:


It would match any number of any characters, but allow at most one space.

respondido 09 mar '12, 14:03

@Gian, no, OP doesn't want to anchor it, as it will be used a part of larger expressions. And also note that your expression would allow two spaces in the target string, try to figure out why. ;-) - Qimpuesto

Ok. About my expression, when I test it I don't manage to match a good result when 2 spaces are entered, are you sure of what you are saying? - GianT971

@Gian, yes, I'm sure. Hint: It has something to do with one of the two characters you added, look it up in los docs. - Qimpuesto

I was thinking of the carriage return, but in my app, I can't have a space and a carriage return with this regexp =/ - GianT971

@Gian, the string " \n" coincidirá. $ always allows a \n at the end of string. - Qimpuesto



Then there may be some specifics depending on other requirements

respondido 09 mar '12, 14:03

here try this:


respondido 09 mar '12, 15:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.