# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"((?<==\h)\([A-Z][A-Z0-9_]+\b(?=[<:\;])|\b(?<!<)[A-Z][A-Z0-9_]+(?=[^>\)]))\b|\b((?<!=\s)(?<!\.)[A-Z]+\d*?(?![\(\;:])(?=[,\)>\s]))\b"
test_str = ("This still has a problem with FOUR+ character match \n"
"\\b(?<!val\\s)(?<!var\\s)(?<!=)(?<!=\\s)(?<!\\.)([A-Z]{1,3}[0-9]*?)\\b\n"
"\\b(?<!val\\s|var\\s|=|=\\s|\\.)([A-Z]+|[A-Z0-9]+)\\b\n"
"\\b([A-Z0-9]{1,3})\\b(?![\\(;:])(?=[,\\)> ])\n"
"<[A-Z0-9]+>|\\([A-Z0-9]+\\)\n"
"\\b([A-Z0-9]{1,3})\\b\n\n"
"<in T, out R>\n"
": R {\n"
": R =\n"
"(S, T, R)\n"
"(S)\n"
"-> S\n"
"-> (S)\n\n"
"[^a-z:\\s\\.\\,]\n\n"
"<>\n"
":\\w+(?=,|>)\n\n"
"//-------------------\n\n"
"class IO<out A>(val fn: () -> A) { // blows the stack\n"
" fun <B> map(g: (A) -> B): IO<B> = IO { g(this()) }\n"
" fun <B> flatMap(g: (A) -> IO<B>): IO<B> = IO { g(this())() }\n\n"
" operator fun invoke(): A = fn()\n\n"
" companion object {\n"
" val empty: IO<Unit> = IO {}\n"
" operator fun <A> invoke(a: A): IO<A> = IO { a }\n"
" }\n"
"}\n\n"
"// blows the stack\n"
"fun <A, B> forever(ioa: IO<A>): IO<B> {\n"
" val t: () -> IO<B> = { forever(ioa) }\n"
" return ioa.flatMap { t() }\n"
"}\n\n"
"var count = 0\n"
"val program: IOU = IO {\n"
" forever<String, String>(IO { \"${count++} Repeating function\" })\n"
" .flatMap { Console.println(it) }\n"
"}\n\n"
"//-----------------------\n\n"
"class State<S, out T>(val runState: (S) -> Pair<S, T>) {\n\n"
" fun <R> map(f: (T) -> R): State<S, R> =\n"
" flatMap { t -> pure<S, R>(f(t)) }\n\n"
" fun <P15, R> map(sx: State<S, P1>, f: (T, P1) -> R): State<STAB, R> =\n"
" flatMap { t -> sx.map { x -> f(t, x) } }\n\n"
" fun <R> flatMap(f: (T) -> State<S, R>): State<S, R> =\n"
" State { s ->\n"
" val (s1, t) = runState(s)\n"
" f(t).runState(s1)\n"
" }\n\n"
" companion object {\n"
" fun <S, T> pure(t: T): State<S, T> =\n"
" State { s -> s to t }\n\n"
" fun <S> get(): State<S, S> =\n"
" State { s -> s to s }\n\n"
" fun <S> set(newState: S): State<S, Unit> =\n"
" State { _ -> newState to Unit }\n\n"
" fun <S> modify(f: (S) -> S): State<S, Unit> =\n"
" get<S>().flatMap { s: S -> set(f(s)).map { Unit } }\n"
" }\n"
"}\n\n"
"//----------------\n\n"
"data class Book(\n"
" val ISDN: String,\n"
" val name: String,\n"
" val pages: Int,\n"
" val price: Price,\n"
" val weight: Double,\n"
" val year: Int,\n"
" val author: String\n"
")\n\n"
"//-------------------\n\n"
"fun <S, A> StateAlias<S, A>.map(f: (A) -> Boolean): StateAlias<S, Boolean> =\n"
" { s ->\n"
" val (s1, t) = this(s)\n"
" when {\n"
" f(s1) -> Pair(true, t)\n"
" else -> Pair(false, t)\n"
" }\n"
" }\n\n"
"fun <S, A, B> flatMap(): ((StateAlias<S, A>) -> (A)) -> ((A) -> (StateAlias<S, B>)) -> StateAlias<S, B>\n\n"
"//--------------------\n\n"
"val dog = Dog(id = 0, name = \"Doglin\", furColour = FurColour.BLACK\n\n"
"val cat: DOG = DOG\n\n"
"//-------------------\n\n"
"fun <T : Comparable<T>> sort(list: List<T>)\n\n"
"fun <T> cloneWhenGreater(list: List<T>, threshold: T): List<T>\n"
" where T : Comparable,\n"
" T : Cloneable {\n"
" return list.filter { it > threshold }.map { it.clone() }\n"
"}\n\n"
"//----------------------\n\n"
"enum class FurColour { BLACK, GREEN, PATCHED; }\n\n"
"enum class Direction {\n"
" NORTH, SOUTH, WEST, EAST\n"
"}\n\n"
"enum class Color(val rgb: Int) {\n"
" RED(0xFF0000),\n"
" GREEN(0x00FF00),\n"
" BLUE(0x0000FF)\n"
"}\n\n"
"enum class ProtocolState {\n"
" WAITING {\n"
" override fun signal() = TALKING\n"
" },\n\n"
" TALKING {\n"
" override fun signal() = WAITING\n"
" };\n\n"
" abstract fun signal(): ProtocolState\n"
"}")
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html