Skip to content

Instantly share code, notes, and snippets.

@clojens
Created December 12, 2013 17:58
Show Gist options
  • Save clojens/7932406 to your computer and use it in GitHub Desktop.
Save clojens/7932406 to your computer and use it in GitHub Desktop.
A few clojure java regex samples
(def ptrn
{
:a {:pattern #"a(?!b)"
:purpose "Only allow a if it is not preceded by a 'b' (negative lookahead)"
:samples ["acdefg" ; ok
"abcdef" ; nil
]}
:b {:pattern #"(?i)(<title.*?>)(.+?)(</title>)"
:purpose "Grouping and backreference to get the text from a HTML <title> tag."
:samples ["titles are just dumb" ; nil
"<title>1337</title>" ; ok
]}
:c {:pattern #"(\w)(\s+)([\.,])"
:purpose "Remove whitespace between word and . or ,"
:samples ["word ." ; ok
"to ," ; ok
"test." ; nil
]}
:d {:pattern #"[a-zA-Z]{3}"
:purpose "Get exactly 3 characters from A to Z in both upper- and lower-case. Not less."
:samples ["FU" ; nil
"John" ; Joh
"Joe" ; Joe
]}
:e {:pattern #"[tT]rue|[yY]es"
:purpose "returns true if the string matches exactly 'true' or 'True' or 'yes' or 'Yes"
:samples ["" ; ?
"" ; ?
"" ; ?
]}
:f {:pattern #"^[^\d].*"
:purpose "Returns true if the string does not have a number at the beginning"
:samples ["1 beer please" ; nil
"More beer for this 1!!" ; ok
"One beer two beer" ; ok
]}
:g {:pattern #"[^0-9]*[12]?[0-9]{1,2}[^0-9]*"
:purpose "Returns true if the string contains a number less then 300"
:samples ["123456789101112134" ; ok
"Someone rented 300" ; ?
"99 bottles of beer" ; ?
]}
:h {:pattern #"([\w&&[^b]])*"
:purpose "Returns true if the string contains a arbitrary number of characters except b"
:samples ["acdefghijklmnopqrstuvwxyz" ; ok
"ABBA was a mid 70's band" ; nil
"Berenbotje kon niet varen" ; nil
]}
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
:X {:pattern #""
:purpose ""
:samples ["" ; ?
"" ; ?
"" ; ?
]}
})
(defn step [k] (map #(re-matches (get-in ptrn [k :pattern]) %) (get-in ptrn [k :samples])))
;; Java built-in regex support in string objects
;; A little threading macro to show how string instances have native
;; methods to deal with regular expressions.
(-> "foo bar" String. (.matches "fo.*"))
(step :h)
@clojens
Copy link
Author

clojens commented Dec 12, 2013

Subexpression Matches
^ Matches beginning of line.
$ Matches end of line.
. Matches any single character except newline. Using m option allows it to match newline as well.
[...] Matches any single character in brackets.
[^...] Matches any single character not in brackets
\A Beginning of entire string
\z End of entire string
\Z End of entire string except allowable final line terminator.
re* Matches 0 or more occurrences of preceding expression.
re+ Matches 1 or more of the previous thing
re? Matches 0 or 1 occurrence of preceding expression.
re{ n} Matches exactly n number of occurrences of preceding expression.
re{ n,} Matches n or more occurrences of preceding expression.
re{ n, m} Matches at least n and at most m occurrences of preceding expression.
a| b Matches either a or b.
(re) Groups regular expressions and remembers matched text.
(?: re) Groups regular expressions without remembering matched text.
(?> re) Matches independent pattern without backtracking.
\w Matches word characters.
\W Matches nonword characters.
\s Matches whitespace. Equivalent to [\t\n\r\f].
\S Matches nonwhitespace.
\d Matches digits. Equivalent to [0-9].
\D Matches nondigits.
\A Matches beginning of string.
\Z Matches end of string. If a newline exists, it matches just before newline.
\z Matches end of string.
\G Matches point where last match finished.
\n Back-reference to capture group number "n"
\b Matches word boundaries when outside brackets. Matches backspace (0x08) when inside brackets.
\B Matches nonword boundaries.
\n, \t, etc. Matches newlines, carriage returns, tabs, etc.
\Q Escape (quote) all characters up to \E
\E Ends quoting begun with \Q

@tinkalgogoi
Copy link

tinkalgogoi commented Aug 9, 2017

Can you please update with a regular expression if we want to check with an ending string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment