ReX.js – Your RegEx companion
areknawo.github.io
areknawo.github.io
// match something that looks like a date in the form 02-14-1924
let x = "";
x += '\d{2}'; // match the month
x += '\-';
x += '\d{2}'; // match the day
x += '\-';
x += '\d{4}'; // match the year
Even this is horribly overcomplicated example would just be `\d{2}\-\d{2}\-\d{4}`.
The Rex library provides no useful abstractions to simplify vanilla javascript and regex.If a (dynamically built) regex misbehaves, I'm only inspecting the lines of code that compose the string, not an entire library to understand what went wrong.
$ node
> '02-14-1924'.match(/\d{2}-\d{2}-\d{4}/)
[ '02-14-1924', index: 0, input: '02-14-1924', groups: undefined ] $ node
> let XRegExp = require('xregexp');
> XRegExp.exec('02-14-1924', XRegExp(`
(?<day> \\d{2} ) -
(?<mon> \\d{2} ) -
(?<year> \\d{4} )
`, 'x'))
[ '02-14-19',
'02',
'14',
'1924',
index: 0,
input: '02-14-1924',
groups: undefined,
day: '02',
mon: '14',
year: '1924' ]Named captures can be created with the following helper (with an example that parses strings):
import {avoid, capture, either, sequence, suffix} from 'compose-regexp'
function makeNamedCaptures(indices) {
let i = 1
return function namedCapture(name, exp) {
if (indices.hasOwnProperty(name)) {
throw new Error("\"" + name + "\" is already used as a capture name")
}
indices[name] = i ++
return capture(exp)
}
}
const captureIndices = {}, namedCapture = makeNamedCaptures(captureIndices)
const zeroPlus = suffix('*')
const any = /[\s\S]/
const anyNonReturn = /./
const stringMatcher = sequence(
namedCapture("quote", /['"]/),
zeroPlus(
avoid(ref(captureIndices.quote)),
either(
sequence('\\', any),
anyNonReturn
)
),
ref(captureIndices.quote)
)
// stringMatcher is actually a plain regexp:
console.log(`"foo'\\"'bar"baz`.match(stringMatcher))
// --> [ '"foo\'\\"\'bar"', '"', index: 0, input: '"foo\'\\"\'bar"baz' ]
You can off course parse strings without using back references, the goal here is to provide an example with a simple grammar that you can follow along.For complex grammar with sub-parts, you can wrap the sb-patterns in functions and inject the `namedCapture` as a parameter.
https://flems.io/#0=N4IgZglgNgpgziAXAbVAOwIYFsZJAOgAsAXLKEAG...
For a regex library, that strikes me as not only not small, but actually extremely big.
If you're building a project that really warrants 8+k just for an external dependency to abstract the regex logic, you're either overusing regex, or regex is such a central and fundamental part of your project that rolling your own non-generalised abstractions is going to be well worth your while.
> As you can see one of main advantages of using this library is ability to document every line of code without any hassle.
In other words, the 'x' flag. Though it turns out JavaScript doesn't provide the x flag, but it's the same thing as string concatenating a regex across different lines.
The fact that the author includes the warning "Never-ever add a semicolon if you want to continue expression" on a method chain shows the level of sophistication this library is intended for.
For the record, I'd never fault anyone for writing any code they want (unless maliciously ofc). "What would a method-based interface to regular expressions look like?" is a perfectly reasonable question! But the author is doing a disservice to anyone he gets to use this. Just use ''.match().
----
Regex syntax sucks, or more exactly, it look like is the bytecode for programming language that is left as exercise to the reader.
This is a truly awful straw-man.
Is totally outside the experience among other DSL. For example, you are on python, your DSLs are pythonic, except this one, that look totally alien.
Learn it, use it, accept it. Or are you trolling?
It is supported by most languages with special syntax because string manipulation is still important. The native support matters due to optimisations present that are peculiar to the runtime. Most any developer can understand your regular expressions, or they know who to ask. You need to know it if you expect to use shell tools.
It's just like SQL: sure SQL is missing modern features and modern syntax, but at least once you've learnt it you can use it for debugging, performance, administrative tasks, reporting systems, dev tools, and your future projects that use another language.
Its only redeeming feature is that it's fast (assuming you don't use the wrong features - lookaheads can cripple performance). Looking for alternative abstractions is very worthwhile. I'm not suggesting throwing away the existing regex engines, just abstracting away the ugliness. Whether this solution is the right one is debatable.
You can draw a comparison with SQL. Lots of projects nowadays hide SQL behind an ORM, only using SQL when a very complex query needs to be written. I'd expect something similar for Regex would help maintainability of lots of projects.
This:
new Matcher().find('Reg').anyWhitespace().capture((expr) => { expr.find('Exp') }).test('Reg Exp')
vs: 'Reg Exp'.match('Reg\s(Exp)')
> Its only redeeming feature is that it's fast (assuming you don't use the wrong features - lookaheads can cripple performance). Looking for alternative abstractions is very worthwhile.First, it's not particular features of regex that are "slow", but the whole notion of backtracking that's central to how they work! Further, regexes are just a translation of a fundamental mathematical model. In university they even had us translate between finite state machines and regular expressions on tests. In short, any "alternate abstraction" you're looking for is going to be literally the same thing, just wordier, because you're talking about something fundamental in computer science.
In case you're interested, here's a finite state machine to regex translator / simulator linked from the wikipedia page on finite state machines: http://ivanzuzak.info/noam/webapps/fsm_simulator/
Maybe for the purpose of a more interesting discussion, let's just assume everybody knows (how to look up the docs for) regular expressions, but sometimes they're still lazy or just feeling playful, and want to do the same stuff with a different syntax... and this is just one first prototype of how maybe to do that. No offense intended, no claim that this will supersede learning regular expressions implied.
In portrait mode, it gets down to just showing the "const expr = new Matcher()" line in the first code sample before going blank white.
If I've got the zoom set to 125%, then portrait is blank white everywhere below the "Github" and "Get Started" buttons.
Reducing zoom to 50% in portrait allows all the page content to be scrolled into view, although continuing to attempt to scroll blanks it out.
This isn't some quirk of my SP4. I tried it with the same results on Microsoft's Win 10 MSEdge VM running under VMWare, available here: https://developer.microsoft.com/en-us/microsoft-edge/tools/v...
Edit: it seems to happen when the class for <body> is changed from "ready-transition ready ready-spinner ready-fix" to "ready-transition ready ready-spinner ready-fix sticky", which seems to happen when it is changing the thingy on the left from scrolling with the page to sticking in place.
When on the blank white page, removing "sticky" from the <body> class via the inspector makes the missing material materialize.
https://spin.atomicobject.com/2013/08/26/verbal-expression/
https://github.com/VerbalExpressions/JSVerbalExpressions
https://medium.com/@leighsn/verbal-expressions-in-ruby-fb8a9...
"Beginner programmers don't understand regex, good programmers understand regex, the best programmers don't use regex."
I've found regex is a poor choice in production systems because it is very hard to maintain (which this library tries to address) and very hard to test (which this library does not address).
However, regex is perfect for that complicated one-time find and replace.
Case in point: logfile analysis. Often times you might have a log file with many different kinds of messages formatted in many different ways, only some of which you care about. A quick regular expression can pull out the messages of interest, and extract out the bits you care about. Sure, in an ideal world you'd ingest the logging data in some structured format, but the real world simply does not hand you the luxury of controlling all your upstream data sources all the time.
I have seen people try to replace regexes with sequences of splits and accesing hardcoded field numbers. This can inmprove readability in the simple cases, but makes code much more fragile in the complex cases.
Another approach I have seen is a full featured lexer. Looks clean and very fadt, but in my experience is an overkill for most tasks
https://github.com/typester/emacs/blob/master/lisp/emacs-lis...
Though then again the nice (best?) thing about macros is it gets inlined as a string at compile time anyways.
Quick typo on the homepage: "extreamly small" should be "extremely small"
If you don't, then go back to designing* I guess, and ask for a programmers' help
* I only say this because I recently saw a tweet from a frontend designer person who said she thought regex was the worst technology ever invented and she absolutely hated it.
Disclaimer: For whatever reason, I'm a big fan. Concisely describe patterns in text and match on them? Plus the puzzle-fun of figuring out how to match on what you're looking for? Yes, please. (And yet, on certain programmer teams, I've had to scale back my regex usage because of you folks who refuse to just learn it, sigh)
I think if more people took the time to understand
them and practice writing them (when appropriate),
they wouldn't have such a stigma.
I think this is part of the movement to a more "ide/web centric" development model. The chances to interaction with regexes on CLI are endless. Grep, git-grep, vim, sed, etc. Even bash has some limited regex integration. As a person who still does most their development in the cli with vim regexes are a massive part of my work flow I actively use them several times per day.When people move away from this model there are a lot opportunities to practice regexes. I've noticed this my own workflow when working Java-centric languages (who's IDE's often have poor regex integration).
Sure, but I don't think the existence of this lib implies the author disputes that, and I can only imagine they're pretty familiar with regex themselves. I think they're trying to fill a legitimate need in frontend dev, which is: if you're only touching regex a few times a year, it's pretty intimidating, and not at all maintainable. SQL can also be difficult to grok if you don't write it often, and ORM interfaces make it easier.
Would the world be a better place if everybody knew regex perfectly? Yeah ok it would, but investing a bunch of time learning something you will only occasionally use is a poor investment IMO.
> If you don't, then go back to designing* I guess, and ask for a programmers' help
This seems to be needlessly condescending? You can be an excellent programmer (particularly in frontend) for years while avoiding regex. It's not a make-or-break thing.
Are you writing APL? Why not?
They are pretty much write-only, and there's no way to compose two regexps safely (new `RegExp(a.source, b.source)` breaks for `/a|A/, /b/, for example).
Rex is a good attempt at solving this. I wrote a similar lib a while ago (more functional and tighter in design, see my other comment on this page).
What if it was all on one line and his variable names were all "v1", "v2", etc. Would you help him then? fuck no. And yet, this is standard operating procedure with regex, except you don't even get "v1", "v2" because nothing is labeled at all. v1/v2/... would be an improvement!
This is how most people write a simple date regex:
\d{1,2}/\d{1,2}/(\d{4}|\d{2})
And mind you, this is a very simple scenario. Here is how you would write it if you treated it like actual code:
(?<month>\d{1,2})
/
(?<day>\d{1,2})
/
(?<year>\d{4}|\d{2})
First off, you can know what my intent is when I'm capturing each group. Maybe this code gets used by a european where the month and day switch places. They can figure out how to fix it in like two seconds. Secondly, the forward slashes are not lost in a sea of characters anymore because we use whitespace like a civilized developer, not a regex savage.
Was this actually necessary to say? It's not only insulting to people who don't need/care about/currently know regex, while simultaneously insulting of designers' intelligence.
But I gotta say, regex isn't the best way to show things. Hard to verify, hard to maintain and what not. Computer sciency, sure. But not well "designed".
Now I have no love lost for people who talk about stuff without understanding the fundamentals or the basics even. So I don't doubt that the person was "whatever". But I do feel that regexes need an overhaul
Ps: I'm a fan too when I have to do quick stuff in shell. But not so much when I save it to a file to be read by me and multiple people weeks later. Regex is one thing that is way easier to write than read.
https://stackoverflow.com/questions/24642616/what-are-extend...