开发者

REGEX for alphabetic character in any alphabet, excluding everything else

I need a regex to match a letter of any alphabet, but not a number or punctuation. I have tried things like:

$match = "~\b(,\s?\b)*$~";

and

$match = "~\w+(,\s?\w+)*$~";

But both let in dashes etc

I even tried to write an exclusion like [^0-9 -+,.*&] but it was a nightmare trying to escape the escaped etc. Any help gratefully received.

Note: This follows on from yesterday's quesiton on the comma seperated list: validate comma separated list using regex

This:

 $value = '[A-Z]+';
$match = "~^$value(,\s?$value)*$~i";

           if (!empty($associations) && !preg_match($match,$associations))

//ret开发者_开发问答urn error, not comma seperated list of words
}
else {
// go ahead
}

But then the client asked that it match umlauts


Try this

$match = "~^\p{L}+(,\s?\p{L}+)*$~u";

\p{L} is a unicode code point in the category letter, no numbers, no punctuation See here on regular-expressions.info

You have to use this together with the modifier u

I recognized it does not work together with the word boundary \b.

Important: To ensure that the complete string is verified the pattern needs to be anchored to the start and the end using ^ and $


UPDATE: answer adjusted to validate a comma separated list

Setting a locale before matching a query works for me:

setlocale(LC_ALL, 'de_DE');
preg_match_all('/(?:^|,\s*)\b([[:alpha:]]+)/', 'Viele, Köche, verderben, den, Brei', $m);
var_export($m);

should yield:

array (
  0 => 
  array (
    0 => 'Viele',
    1 => ', Köche',
    2 => ', verderben',
    3 => ', den',
    4 => ', Brei',
  ),
  1 => 
  array (
    0 => 'Viele',
    1 => 'Köche',
    2 => 'verderben',
    3 => 'den',
    4 => 'Brei',
  ),
)


'the solution of your problem is write below

'Regular expression:

"[^a-zA-Z]"

'Regular expression for accepting characters only in C#.net


    public bool IsAlpha(String strToCheck)
       {

    Regex objAlphaPattern=new Regex("[^a-zA-Z]");

   return !objAlphaPattern.IsMatch(strToCheck); 

        }


     'Regular expression for accepting characters only in vb.net



      Public Function IsAlpha(ByVal strToCheck As String) As Boolean

         Dim objAlphaPattern As Regex = New Regex("[^a-zA-Z]")

          Return Not objAlphaPattern.IsMatch(strToCheck)

       End Function



     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)        Handles Button1.Click

    If IsAlpha(TextBox1.Text) = True Then
        Label1.Text = "Valid"
    Else
        Label1.Text = "Invalid"
    End If
End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜