2

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))

//return error, not comma seperated list of words
}
else {
// go ahead
}

But then the client asked that it match umlauts

Community
  • 1
  • 1
Liam Bailey
  • 5,879
  • 3
  • 34
  • 46
  • [Read this question](http://stackoverflow.com/questions/6407983/utf-8-in-php-regular-expressions). You need to use the /u UTF8 modifier since you want characters outside the ASCII range. – Dan Grossman Jul 27 '11 at 09:00
  • That is the obvious answer, and the first thing I tried, except you need to match UTF-8 codes which is a nightmare – Liam Bailey Jul 27 '11 at 10:53

3 Answers3

3

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 $

stema
  • 90,351
  • 20
  • 107
  • 135
  • This is good, but it allows: alt - älter - am ältesten as a comma seperated list. – Liam Bailey Jul 27 '11 at 10:59
  • @Liam I don't understand. Why don't you post example strings to match (and some that should not be matched). What do you mean with "alt - älter - am ältesten as a comma seperated list"? Should it match "alt, älter, am ältesten"? If yes, my solution will not, because there is no space allowed apart from directly after the comma. – stema Jul 27 '11 at 11:18
  • I am trying to ensure that users enter a comma seperated string of single words, no spaces, no punctuation and no numbers, just single words like: hand, tree, wood, work. Therefore alt - älter - am ältesten is going through when it shouldn't be – Liam Bailey Jul 27 '11 at 12:55
  • @Liam, now I understand and now the mistake is obvious. You need to anchor the pattern to the start of the string using a `^`. Otherwise it will ignore the start of the string and match only the last word "ältesten" and the result is OK. I updated my answer accordingly. – stema Jul 27 '11 at 13:04
0

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',
  ),
)
Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
  • Good answer, but I really need this to work in a comma seperated list, see question from yesterday and what I have added to bottom. – Liam Bailey Jul 27 '11 at 09:35
  • It works perfectly on comma seperated lists, but it also allows alt - älter - am ältesten as a comma seperated list. I am using this to reject strings that are not comma seperated lists. – Liam Bailey Jul 27 '11 at 11:13
  • So you want to reject strings, that aren't comma separated *or* are comma separated but contain non-alpha characters? – Linus Kleen Jul 27 '11 at 11:41
  • Then `if (!preg_match(...)) reject()` using the regular expression from my answer should work to reject a string, no? – Linus Kleen Jul 27 '11 at 12:56
  • Thanks, but I already had it set for that, and simply added your regex. It didn't do what I needed. – Liam Bailey Jul 27 '11 at 14:39
-3

'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
humrahimcs
  • 11
  • 2
  • -1 I don't see how this helps the OP with his question, apart from that its something in c#/vb.net and he is in PHP. – stema Jul 27 '11 at 11:22