Who is afraid of regex?
-
(I'm ignoring backtracking regex here because it's dirty, and algorithmically less useful except for making it easier for the user to match text) Anyway it's just a tiny functional programming language with only ()|?* 4 explicit operators and 1 implicit one. Representing the regex programming language as code: Any regex is mathematically equivelent to the DFA state machine it represents, and can be converted algorithmically back and forth to and from a state machine and a regular expression. Perfect compilation/decompilation. So you can use them to match text (boring!) Or you can use them to generate code for state machines (less boring!) And yet I've met a lot of programmers that either loathe them, are intimidated by them, or both. They're wonderful little things, with interesting mathematical properties, but more importantly, they're useful for everything quick and dirty.
Real programmers use butterflies
I would rather use whatever language I am working with to perform the parse. As you just stated... regex is technically another small programming language. I am not sure if you know this... but you can take a regular expression and use the Ragel state machine compiler[^] to convert it to C/C++, D, Go, Java, Ruby and even Objective-C. Interestingly... I do not see C# support. Ragel Cheat Sheet[^] Someone should probably write a little Visual Studio addon that takes a regular expression and converts it to a C# state machine... as it seems .NET programmers use alot of regex. Best Wishes, -David Delaune
-
(I'm ignoring backtracking regex here because it's dirty, and algorithmically less useful except for making it easier for the user to match text) Anyway it's just a tiny functional programming language with only ()|?* 4 explicit operators and 1 implicit one. Representing the regex programming language as code: Any regex is mathematically equivelent to the DFA state machine it represents, and can be converted algorithmically back and forth to and from a state machine and a regular expression. Perfect compilation/decompilation. So you can use them to match text (boring!) Or you can use them to generate code for state machines (less boring!) And yet I've met a lot of programmers that either loathe them, are intimidated by them, or both. They're wonderful little things, with interesting mathematical properties, but more importantly, they're useful for everything quick and dirty.
Real programmers use butterflies
regex is a tool. regex is great when used correctly. read the owner's manual, as with any tool. no one should be afraid of using a tool to complete a specific task.
-
(I'm ignoring backtracking regex here because it's dirty, and algorithmically less useful except for making it easier for the user to match text) Anyway it's just a tiny functional programming language with only ()|?* 4 explicit operators and 1 implicit one. Representing the regex programming language as code: Any regex is mathematically equivelent to the DFA state machine it represents, and can be converted algorithmically back and forth to and from a state machine and a regular expression. Perfect compilation/decompilation. So you can use them to match text (boring!) Or you can use them to generate code for state machines (less boring!) And yet I've met a lot of programmers that either loathe them, are intimidated by them, or both. They're wonderful little things, with interesting mathematical properties, but more importantly, they're useful for everything quick and dirty.
Real programmers use butterflies
-
I would rather use whatever language I am working with to perform the parse. As you just stated... regex is technically another small programming language. I am not sure if you know this... but you can take a regular expression and use the Ragel state machine compiler[^] to convert it to C/C++, D, Go, Java, Ruby and even Objective-C. Interestingly... I do not see C# support. Ragel Cheat Sheet[^] Someone should probably write a little Visual Studio addon that takes a regular expression and converts it to a C# state machine... as it seems .NET programmers use alot of regex. Best Wishes, -David Delaune
-
I am afraid of you, messing up with regex. :-D
Quote:
Or you can use them to generate code for state machines
That's indeed intriguing.
"In testa che avete, Signor di Ceprano?" -- Rigoletto
I wrote an article (which people hated) where I tried to impress that upon the reader. Fun With State Machines: Incrementally Parsing Numbers Using Hacked Regex[^] It centered around this expression for a JSON number:
(\-?)(0|[1-9][0-9]*)((\.[0-9]+)?([Ee][\+\-]?[0-9]+)?)
Which generates this state graph: State Graph[^] Which I then use to generate a state machine.Real programmers use butterflies
-
I would rather use whatever language I am working with to perform the parse. As you just stated... regex is technically another small programming language. I am not sure if you know this... but you can take a regular expression and use the Ragel state machine compiler[^] to convert it to C/C++, D, Go, Java, Ruby and even Objective-C. Interestingly... I do not see C# support. Ragel Cheat Sheet[^] Someone should probably write a little Visual Studio addon that takes a regular expression and converts it to a C# state machine... as it seems .NET programmers use alot of regex. Best Wishes, -David Delaune
I could do that, since I already wrote several apps that do exactly that. The issue is it doesn't support backtracking because i don't like backtracking regex.
Real programmers use butterflies
-
Regex is black magic and sacrifices of caffeine and pizza offered in copious amounts is the only way to appease the beast.
I'm not sure how many cookies it makes to be happy, but so far it's not 27. JaxCoder.com
But it's such useful, compelling black magic! ;P
Real programmers use butterflies
-
(I'm ignoring backtracking regex here because it's dirty, and algorithmically less useful except for making it easier for the user to match text) Anyway it's just a tiny functional programming language with only ()|?* 4 explicit operators and 1 implicit one. Representing the regex programming language as code: Any regex is mathematically equivelent to the DFA state machine it represents, and can be converted algorithmically back and forth to and from a state machine and a regular expression. Perfect compilation/decompilation. So you can use them to match text (boring!) Or you can use them to generate code for state machines (less boring!) And yet I've met a lot of programmers that either loathe them, are intimidated by them, or both. They're wonderful little things, with interesting mathematical properties, but more importantly, they're useful for everything quick and dirty.
Real programmers use butterflies
Reminds me of APL. The language of 80's modelling gods. Cryptic enough they had their own department. At least in assembler we had a few letters for op codes. Afraid? It's just too forgettable (for me).
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food
-
(I'm ignoring backtracking regex here because it's dirty, and algorithmically less useful except for making it easier for the user to match text) Anyway it's just a tiny functional programming language with only ()|?* 4 explicit operators and 1 implicit one. Representing the regex programming language as code: Any regex is mathematically equivelent to the DFA state machine it represents, and can be converted algorithmically back and forth to and from a state machine and a regular expression. Perfect compilation/decompilation. So you can use them to match text (boring!) Or you can use them to generate code for state machines (less boring!) And yet I've met a lot of programmers that either loathe them, are intimidated by them, or both. They're wonderful little things, with interesting mathematical properties, but more importantly, they're useful for everything quick and dirty.
Real programmers use butterflies
By Law we need to quote this XKCD. xkcd: Regular Expressions[^] I used to have this T-shirt, too: Regular Expressions[^] Personally regular expressions are my indulgent cheat. Kinda like having pizza. I know I should go easy on them, and I'm trying to give them up, but when they are good, they are sooo good.
cheers Chris Maunder
-
Reminds me of APL. The language of 80's modelling gods. Cryptic enough they had their own department. At least in assembler we had a few letters for op codes. Afraid? It's just too forgettable (for me).
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food
I built a regex DOM for people like you. :suss: :laugh:
Real programmers use butterflies
-
But it's such useful, compelling black magic! ;P
Real programmers use butterflies
That it is, I've used Expresso Regular Expression Tool[^] for years. It helps but I still can't wrap my head around it. That and dark matter... :)
I'm not sure how many cookies it makes to be happy, but so far it's not 27. JaxCoder.com
-
By Law we need to quote this XKCD. xkcd: Regular Expressions[^] I used to have this T-shirt, too: Regular Expressions[^] Personally regular expressions are my indulgent cheat. Kinda like having pizza. I know I should go easy on them, and I'm trying to give them up, but when they are good, they are sooo good.
cheers Chris Maunder
I would wear that shirt but then people would ask me what it meant and if I told them they would ask me to fix their computers. Apparently I have no impulse control because I even use regular expressions for things they were never intended for. :-\
Real programmers use butterflies
-
That it is, I've used Expresso Regular Expression Tool[^] for years. It helps but I still can't wrap my head around it. That and dark matter... :)
I'm not sure how many cookies it makes to be happy, but so far it's not 27. JaxCoder.com
Forget backtracking regular expressions, as they don't have the same fancy mathematical properties as their non-backtracking counterparts. Use the non-backtracking operators and there's only 5 operations to remember, concatenation, alternation, parentheses, zero or one match and kleene star (looping * - zero or more match), and concatenation is implicit. They are 1. Simpler to understand 2. Faster to execute 3. Weirdly mathy but in a cool way 4. The same across almost all regular expression engines I give a primer at the end of this article. I taught them to my computer, and trust me - it's not very smart, but then I also taught it C in that article. Fun With State Machines: Incrementally Parsing Numbers Using Hacked Regex[^]
Real programmers use butterflies
-
By Law we need to quote this XKCD. xkcd: Regular Expressions[^] I used to have this T-shirt, too: Regular Expressions[^] Personally regular expressions are my indulgent cheat. Kinda like having pizza. I know I should go easy on them, and I'm trying to give them up, but when they are good, they are sooo good.
cheers Chris Maunder
-
(I'm ignoring backtracking regex here because it's dirty, and algorithmically less useful except for making it easier for the user to match text) Anyway it's just a tiny functional programming language with only ()|?* 4 explicit operators and 1 implicit one. Representing the regex programming language as code: Any regex is mathematically equivelent to the DFA state machine it represents, and can be converted algorithmically back and forth to and from a state machine and a regular expression. Perfect compilation/decompilation. So you can use them to match text (boring!) Or you can use them to generate code for state machines (less boring!) And yet I've met a lot of programmers that either loathe them, are intimidated by them, or both. They're wonderful little things, with interesting mathematical properties, but more importantly, they're useful for everything quick and dirty.
Real programmers use butterflies
The only regex(-like) syntax I felt somewhat comfortable working with was SNOBOL :-) That was 30+ years ago. I first met it as a 200 source lines version of Eliza, the therapist, which fascinated me immensely. Obviusly, that version never passed any Turing test, yet: Try to write anything comparable in 200 lines of any ordinary, algorithmic language! So I started playing around with it, just for fun - I never used it commercially. Actually, not too long ago I picked up the source code of an old SNOBOL interpreter, hoping one day to port it. It is currently #43 on my project lists. Tuits are hard to find nowadays, especially round ones.
-
That it is, I've used Expresso Regular Expression Tool[^] for years. It helps but I still can't wrap my head around it. That and dark matter... :)
I'm not sure how many cookies it makes to be happy, but so far it's not 27. JaxCoder.com
Mike Hankey wrote:
dark matter
Dark Matter[^]; great series. A shame it only went three seasons.
Software Zen:
delete this;
-
(I'm ignoring backtracking regex here because it's dirty, and algorithmically less useful except for making it easier for the user to match text) Anyway it's just a tiny functional programming language with only ()|?* 4 explicit operators and 1 implicit one. Representing the regex programming language as code: Any regex is mathematically equivelent to the DFA state machine it represents, and can be converted algorithmically back and forth to and from a state machine and a regular expression. Perfect compilation/decompilation. So you can use them to match text (boring!) Or you can use them to generate code for state machines (less boring!) And yet I've met a lot of programmers that either loathe them, are intimidated by them, or both. They're wonderful little things, with interesting mathematical properties, but more importantly, they're useful for everything quick and dirty.
Real programmers use butterflies
I like using regex for day-to-day, throwaway things. It's especially good for reformatting text. I'm certainly not intimidated by them. That said, I don't think I would ever use one in product code with a long life-span. You must admit that regular expressions tend to be write-only, which is a cardinal sin against those who must maintain the code, including your future selves. Code written very concisely, and regular expressions may be the ultimate in concise, require a lot of mental unpacking during maintenance. Unless you write a ridiculous amount of comments for the expression, it might not be worth it.
Software Zen:
delete this;
-
I like using regex for day-to-day, throwaway things. It's especially good for reformatting text. I'm certainly not intimidated by them. That said, I don't think I would ever use one in product code with a long life-span. You must admit that regular expressions tend to be write-only, which is a cardinal sin against those who must maintain the code, including your future selves. Code written very concisely, and regular expressions may be the ultimate in concise, require a lot of mental unpacking during maintenance. Unless you write a ridiculous amount of comments for the expression, it might not be worth it.
Software Zen:
delete this;
I think it depends. I generally agree that complicated regex is mug's game. However, How do you technically, and accurately convey a set of rules around lexical requirements? Such rules must be able to be conveyed to other developers precisely. Such rules must be unambiguous, and testable. Such rules must be absorbable in reasonable amount of time, meaning no poring over RFCs if one can avoid it. Imagine conveying the rules for what constitutes a JSON number You can either say:
(\-?)(0|[1-9][0-9]*)((\.[0-9]+)?([Ee][\+\-]?[0-9]+)?)
Which takes some unpacking as you say, but is certainly readable. Or I can give you a page long document of requirements around JSON number parsing. Personally, I can read that quite easily, but that's me. Let me propose something - there is a meaningful subset of regular expressions which are easy to understand, and can fulfill most simple lexical specifications like the above, or say, like an email address, or an url, or any number of small, structured text fragments. It beats the alternative, hands down.
Real programmers use butterflies
-
(I'm ignoring backtracking regex here because it's dirty, and algorithmically less useful except for making it easier for the user to match text) Anyway it's just a tiny functional programming language with only ()|?* 4 explicit operators and 1 implicit one. Representing the regex programming language as code: Any regex is mathematically equivelent to the DFA state machine it represents, and can be converted algorithmically back and forth to and from a state machine and a regular expression. Perfect compilation/decompilation. So you can use them to match text (boring!) Or you can use them to generate code for state machines (less boring!) And yet I've met a lot of programmers that either loathe them, are intimidated by them, or both. They're wonderful little things, with interesting mathematical properties, but more importantly, they're useful for everything quick and dirty.
Real programmers use butterflies
I make what could be regarded as "heroic effort" to avoid using regex whenever possible. However, I used it in a recent application because it was the most expedient way to do what I needed.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
I think it depends. I generally agree that complicated regex is mug's game. However, How do you technically, and accurately convey a set of rules around lexical requirements? Such rules must be able to be conveyed to other developers precisely. Such rules must be unambiguous, and testable. Such rules must be absorbable in reasonable amount of time, meaning no poring over RFCs if one can avoid it. Imagine conveying the rules for what constitutes a JSON number You can either say:
(\-?)(0|[1-9][0-9]*)((\.[0-9]+)?([Ee][\+\-]?[0-9]+)?)
Which takes some unpacking as you say, but is certainly readable. Or I can give you a page long document of requirements around JSON number parsing. Personally, I can read that quite easily, but that's me. Let me propose something - there is a meaningful subset of regular expressions which are easy to understand, and can fulfill most simple lexical specifications like the above, or say, like an email address, or an url, or any number of small, structured text fragments. It beats the alternative, hands down.
Real programmers use butterflies
To my mind that regex would be okay. It's the thousands of characters, wall-of-text abominations that I object to. I know, that's an example of poor use of regex, but it's the kind of thing you find. Inexperienced folks start using it, and all of a sudden it becomes their favorite toy. A toy that's all sharp edges...
Software Zen:
delete this;