Skip to main content
Tweeted twitter.com/StackSoftEng/status/1330390574491443201
Put back scope explanation. Some people are commenting that there is no guarantee for the output to match the pattern, which was the point in the first place.
Source Link

I've often come across situations where pattern matching in a string is formalized, but the reverse is not.

Say I invent a new string pattern that can be expressed by regex /([0-9a-z])*:([0-9a-z])*@([0-9a-z])/.

Expressing that regex as data to your friendly local programming language, the standard lib will be able to convert foo"foo:bar@4040404bar@4040404" into ["foo", "bar", "4040404"].

How about converting it the other way? With the same piece of data, the above regex, turning ["bar", "baz", "5050505"] into "bar:baz@5050505".

Based on what I've done so far, I will need to duplicate the pattern—into, for example: "#{m[0]}:#{m[1]}@#{m[2]}".

In other words, can we produce text out of structured data and regex patterns, using the same pattern data for parsing as to the other way?

The other way for which we also have a lot of tools already.—One might call that templating or compiling.—What if the format is the same going both ways? Can we reuse the same codepattern?

(That the outputted string doesn't match the pattern is not important to my question. This is about the reversibility of pattern matching without duplicating pattern data.)

I've often come across situations where pattern matching in a string is formalized, but the reverse is not.

Say I invent a new string pattern that can be expressed by regex /([0-9a-z])*:([0-9a-z])*@([0-9a-z])/.

Expressing that regex as data to your friendly local programming language, the standard lib will be able to convert foo:bar@4040404 into ["foo", "bar", "4040404"].

How about converting it the other way? Based on what I've done so far, I will need to duplicate the pattern—into, for example: "#{m[0]}:#{m[1]}@#{m[2]}".

In other words, can we produce text out of structured data and regex patterns, using the same data for parsing as to the other way?

The other way for which we also have a lot of tools already.—One might call that templating or compiling.—What if the format is the same going both ways? Can we reuse the same code?

I've often come across situations where pattern matching in a string is formalized, but the reverse is not.

Say I invent a new string pattern that can be expressed by regex /([0-9a-z])*:([0-9a-z])*@([0-9a-z])/.

Expressing that regex as data to your friendly local programming language, the standard lib will be able to convert "foo:bar@4040404" into ["foo", "bar", "4040404"].

How about converting it the other way? With the same piece of data, the above regex, turning ["bar", "baz", "5050505"] into "bar:baz@5050505".

Based on what I've done so far, I will need to duplicate the pattern—into, for example: "#{m[0]}:#{m[1]}@#{m[2]}".

In other words, can we produce text out of structured data and regex patterns, using the same pattern data for parsing as to the other way?

The other way for which we also have a lot of tools already.—One might call that templating or compiling.—What if the format is the same going both ways? Can we reuse the same pattern?

(That the outputted string doesn't match the pattern is not important to my question. This is about the reversibility of pattern matching without duplicating pattern data.)

Removed dictionary reverse-lookup request, and small detail which didn't seem to contribute to the question.
Source Link
Robert Harvey
  • 200.7k
  • 55
  • 470
  • 683

I've often come across situations where pattern matching in a string is formalized, but the reverse is not.

Say I invent a new string pattern that can be expressed by regex /([0-9a-z])*:([0-9a-z])*@([0-9a-z])/.

Expressing that regex as data to your friendly local programming language, the standard lib will be able to convert foo:bar@4040404 into ["foo", "bar", "4040404"].

How about converting it the other way? Based on what I've done so far, I will need to duplicate the pattern—into, for example: "#{m[0]}:#{m[1]}@#{m[2]}".

In other words, can we produce text out of structured data and regex patterns, using the same data for parsing as to the other way?

The other way for which we also have a lot of tools already.—One might call that templating or compiling.—What if the format is the same going both ways? Can we reuse the same code?

(That the outputted string matches the pattern is not necessarily important to my question.)

Does that have a name? Is that pattern seen in programming constructs?

I've often come across situations where pattern matching in a string is formalized, but the reverse is not.

Say I invent a new string pattern that can be expressed by regex /([0-9a-z])*:([0-9a-z])*@([0-9a-z])/.

Expressing that regex as data to your friendly local programming language, the standard lib will be able to convert foo:bar@4040404 into ["foo", "bar", "4040404"].

How about converting it the other way? Based on what I've done so far, I will need to duplicate the pattern—into, for example: "#{m[0]}:#{m[1]}@#{m[2]}".

In other words, can we produce text out of structured data and regex patterns, using the same data for parsing as to the other way?

The other way for which we also have a lot of tools already.—One might call that templating or compiling.—What if the format is the same going both ways? Can we reuse the same code?

(That the outputted string matches the pattern is not necessarily important to my question.)

Does that have a name? Is that pattern seen in programming constructs?

I've often come across situations where pattern matching in a string is formalized, but the reverse is not.

Say I invent a new string pattern that can be expressed by regex /([0-9a-z])*:([0-9a-z])*@([0-9a-z])/.

Expressing that regex as data to your friendly local programming language, the standard lib will be able to convert foo:bar@4040404 into ["foo", "bar", "4040404"].

How about converting it the other way? Based on what I've done so far, I will need to duplicate the pattern—into, for example: "#{m[0]}:#{m[1]}@#{m[2]}".

In other words, can we produce text out of structured data and regex patterns, using the same data for parsing as to the other way?

The other way for which we also have a lot of tools already.—One might call that templating or compiling.—What if the format is the same going both ways? Can we reuse the same code?

Source Link

Are there known constructs for two-way string pattern matching?

I've often come across situations where pattern matching in a string is formalized, but the reverse is not.

Say I invent a new string pattern that can be expressed by regex /([0-9a-z])*:([0-9a-z])*@([0-9a-z])/.

Expressing that regex as data to your friendly local programming language, the standard lib will be able to convert foo:bar@4040404 into ["foo", "bar", "4040404"].

How about converting it the other way? Based on what I've done so far, I will need to duplicate the pattern—into, for example: "#{m[0]}:#{m[1]}@#{m[2]}".

In other words, can we produce text out of structured data and regex patterns, using the same data for parsing as to the other way?

The other way for which we also have a lot of tools already.—One might call that templating or compiling.—What if the format is the same going both ways? Can we reuse the same code?

(That the outputted string matches the pattern is not necessarily important to my question.)

Does that have a name? Is that pattern seen in programming constructs?