Regular expression - string contains a “*” in between every alphabet, but starting and ending with alphabet only

时间:2017-06-15 09:44:10

标签: c# regex

if(Regex.IsMatch(c1,@"^[A-Za-z]([\*][a-z])*[A-Za-z]+$"))

    {
        return true;
    }

I am trying to write regular expression which specifies that text should start with a letter and end with a letter and it contains "*" in between every alphabet. I don't know how to specify special character in between every alphabet.

2 个答案:

答案 0 :(得分:1)

This should do:

^[a-zA-Z](?:\*[a-zA-Z])+$

This will look for the first character at the start of the string, and then look for all combinations of * followed by a letter.

Demo

答案 1 :(得分:0)

If think you are looking for:

^[A-Za-z](?:(?:\*[a-z])*\*[A-Za-z])?$

if uppercase letters are allowed in the middle of the string, you can use this funny way:

^(?!.*\B)[A-Za-z*]+$

(The negative lookahead (?!.*\B) prevents the string to contain a non-word boundary)