Ant path style patterns

Ant-style path patterns matching in spring-framework: The mapping matches URLs using the following rules: ? matches one character * matches zero or more characters ** matches zero or more ‘directories’ in a path {spring:[a-z]+} matches the regexp [a-z]+ as a path variable named “spring” Some examples: com/t?st.jsp – matches com/test.jsp but also com/tast.jsp or com/txst.jsp … Read more

What is ‘Pattern Matching’ in functional languages?

Understanding pattern matching requires explaining three parts: Algebraic data types. What pattern matching is Why its awesome. Algebraic data types in a nutshell ML-like functional languages allow you define simple data types called “disjoint unions” or “algebraic data types”. These data structures are simple containers, and can be recursively defined. For example: type ‘a list … Read more

How to select lines between two marker patterns which may occur multiple times with awk/sed

Use awk with a flag to trigger the print when necessary: $ awk ‘/abc/{flag=1;next}/mno/{flag=0}flag’ file def1 ghi1 jkl1 def2 ghi2 jkl2 How does this work? /abc/ matches lines having this text, as well as /mno/ does. /abc/{flag=1;next} sets the flag when the text abc is found. Then, it skips the line. /mno/{flag=0} unsets the flag … Read more

Javascript fuzzy search that makes sense [closed]

I tried using existing fuzzy libraries like fuse.js and also found them to be terrible, so I wrote one which behaves basically like sublime’s search. https://github.com/farzher/fuzzysort The only typo it allows is a transpose. It’s pretty solid (1k stars, 0 issues), very fast, and handles your case easily: fuzzysort.go(‘int’, [‘international’, ‘splint’, ‘tinder’]) // [{highlighted: ‘*int*ernational’, … Read more

What does `:_*` (colon underscore star) do in Scala?

It “splats”1 the sequence. Look at the constructor signature new Elem(prefix: String, label: String, attributes: MetaData, scope: NamespaceBinding, child: Node*) which is called as new Elem(prefix, label, attributes, scope, child1, child2, … childN) but here there is only a sequence, not child1, child2, etc. so this allows the result sequence to be used as the … Read more

How can I tell if a string repeats itself in Python?

Here’s a concise solution which avoids regular expressions and slow in-Python loops: def principal_period(s): i = (s+s).find(s, 1, -1) return None if i == -1 else s[:i] See the Community Wiki answer started by @davidism for benchmark results. In summary, David Zhang’s solution is the clear winner, outperforming all others by at least 5x for … Read more