2

I'm trying to use a regex to match a pattern such as (letter(letter|number))*, I mean a string with infinite length but with a "start with a letter, then be either a letter or a number" rule.

So I'm using this pattern #"\w+[\w|\d]*" in clojure, but if I use just a number it validates, just like the code below, what am I doing wrong?

(re-matches #"\w+[\w|\d]*" "1")

3 Answers 3

5

The other answers look good for the question you asked, but it sounds like you really might be looking to validate identifiers. Note that the Java API provides some useful utility methods to do just that. Sometimes being explicit is better than a regex.

e.g.

(defn identifier? [s]
    (and (Character/isJavaIdentifierStart (first s))
         (empty? (remove #(Character/isJavaIdentifierPart %) (rest s)))))

In java 6 and later you can do this:

(defn identifier? [s]
    (and (not (javax.lang.model.SourceVersion/isKeyword s))
         (javax.lang.model.SourceVersion/isIdentifier s)))
Sign up to request clarification or add additional context in comments.

Comments

5

The token \w matches both letters and numbers. From the documentation:

\w      A word character: [a-zA-Z_0-9]

Use [a-z], \p{Lower}, \p{Upper} or \p{Alpha} for letters:

[a-z][a-z0-9]*

(Also, note that the + doesn't make a difference.)

Comments

4

\w matches letters or digits:

\w A word character: [a-zA-Z_0-9]

http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

So change your regex to: [a-zA-Z]\w*

4 Comments

Thank's, I thought \w was only for letters =S
"word" characters aren't really intended for words, they're intended to match valid characters in "identifiers" in java/c style languages
@JoostDiepenmaat, not really, since those usually include for instance _ and $.
\w matches _, and $ (probably?) isn't a legal identifier character in C. I suspect \w matches legal C identifiers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.