use strict;
my $str = 'This still has a problem with FOUR+ character match
\\b(?<!val\\s)(?<!var\\s)(?<!=)(?<!=\\s)(?<!\\.)([A-Z]{1,3}[0-9]*?)\\b
\\b(?<!val\\s|var\\s|=|=\\s|\\.)([A-Z]+|[A-Z0-9]+)\\b
\\b([A-Z0-9]{1,3})\\b(?![\\(;:])(?=[,\\)> ])
<[A-Z0-9]+>|\\([A-Z0-9]+\\)
\\b([A-Z0-9]{1,3})\\b
<in T, out R>
: R {
: R =
(S, T, R)
(S)
-> S
-> (S)
[^a-z:\\s\\.\\,]
<>
:\\w+(?=,|>)
//-------------------
class IO<out A>(val fn: () -> A) { // blows the stack
fun <B> map(g: (A) -> B): IO<B> = IO { g(this()) }
fun <B> flatMap(g: (A) -> IO<B>): IO<B> = IO { g(this())() }
operator fun invoke(): A = fn()
companion object {
val empty: IO<Unit> = IO {}
operator fun <A> invoke(a: A): IO<A> = IO { a }
}
}
// blows the stack
fun <A, B> forever(ioa: IO<A>): IO<B> {
val t: () -> IO<B> = { forever(ioa) }
return ioa.flatMap { t() }
}
var count = 0
val program: IOU = IO {
forever<String, String>(IO { "${count++} Repeating function" })
.flatMap { Console.println(it) }
}
//-----------------------
class State<S, out T>(val runState: (S) -> Pair<S, T>) {
fun <R> map(f: (T) -> R): State<S, R> =
flatMap { t -> pure<S, R>(f(t)) }
fun <P15, R> map(sx: State<S, P1>, f: (T, P1) -> R): State<STAB, R> =
flatMap { t -> sx.map { x -> f(t, x) } }
fun <R> flatMap(f: (T) -> State<S, R>): State<S, R> =
State { s ->
val (s1, t) = runState(s)
f(t).runState(s1)
}
companion object {
fun <S, T> pure(t: T): State<S, T> =
State { s -> s to t }
fun <S> get(): State<S, S> =
State { s -> s to s }
fun <S> set(newState: S): State<S, Unit> =
State { _ -> newState to Unit }
fun <S> modify(f: (S) -> S): State<S, Unit> =
get<S>().flatMap { s: S -> set(f(s)).map { Unit } }
}
}
//----------------
data class Book(
val ISDN: String,
val name: String,
val pages: Int,
val price: Price,
val weight: Double,
val year: Int,
val author: String
)
//-------------------
fun <S, A> StateAlias<S, A>.map(f: (A) -> Boolean): StateAlias<S, Boolean> =
{ s ->
val (s1, t) = this(s)
when {
f(s1) -> Pair(true, t)
else -> Pair(false, t)
}
}
fun <S, A, B> flatMap(): ((StateAlias<S, A>) -> (A)) -> ((A) -> (StateAlias<S, B>)) -> StateAlias<S, B>
//--------------------
val dog = Dog(id = 0, name = "Doglin", furColour = FurColour.BLACK
val cat: DOG = DOG
//-------------------
fun <T : Comparable<T>> sort(list: List<T>)
fun <T> cloneWhenGreater(list: List<T>, threshold: T): List<T>
where T : Comparable,
T : Cloneable {
return list.filter { it > threshold }.map { it.clone() }
}
//----------------------
enum class FurColour { BLACK, GREEN, PATCHED; }
enum class Direction {
NORTH, SOUTH, WEST, EAST
}
enum class Color(val rgb: Int) {
RED(0xFF0000),
GREEN(0x00FF00),
BLUE(0x0000FF)
}
enum class ProtocolState {
WAITING {
override fun signal() = TALKING
},
TALKING {
override fun signal() = WAITING
};
abstract fun signal(): ProtocolState
}';
my $regex = qr/((?<==\h)\([A-Z][A-Z0-9_]+\b(?=[<:\;])|\b(?<!<)[A-Z][A-Z0-9_]+(?=[^>\)]))\b|\b((?<!=\s)(?<!\.)[A-Z]+\d*?(?![\(\;:])(?=[,\)>\s]))\b/mp;
if ( $str =~ /$regex/g ) {
print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
# print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
# print "Capture Group 2 is $2 ... and so on\n";
}
# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}
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 Perl, please visit: http://perldoc.perl.org/perlre.html