【Perl】Subroutines in regex?【Amateur】
1
Name:
Anonymous
2011-04-17 3:58
Can I use subroutines in regex?
For example, is it possible to do something like:
$string =~ /<Text (example)>/<Text convert_text($1)>/g;
I know that doesn't work like that, but is there a way to do something like that?
2
Name:
Anonymous
2011-04-17 4:18
Subroutines have the expressive capabilities of a Turing-machine. It would no longer be a regular grammar if you could do that.
3
Name:
Anonymous
2011-04-17 4:32
If you need a sub in your REGEX than something larger is incorrect.
4
Name:
Anonymous
2011-04-17 5:20
So if I wanted to parse a link, such as This is a link to be <a href="This_is_a_link">This is a link</a> I couldn't do that with one substitution?
5
Name:
Anonymous
2011-04-17 5:23
Right bud- that's "doing it wrong." At least I'd say so.
6
Name:
Anonymous
2011-04-17 5:24
>>2
It depends on what you mean by "subroutines". Regular expressions (or rather, finite state automatons) are capable of carrying out any computation which requires only constant amount of memory.
7
Name:
Anonymous
2011-04-17 5:27
>>4
What are you making the RegEx in? PHP?
8
Name:
Anonymous
2011-04-17 5:30
If ur subing this in the PHP it is fine, and yes, should be done in that level of code.
9
Name:
Anonymous
2011-04-17 5:31
>>6
I think it's safe to assume that subroutines mean arbitrary functions in the programming language, which would be Turing-complete.
>>4
It is a mistake to parse HTML with regular grammars to begin with.
10
Name:
Anonymous
2011-04-17 5:31
!!!Be Aware!!! - Your custom code can Fuck UP future code.
11
Name:
Anonymous
2011-04-17 5:36
>>2
Perl's regexp are Turing-complete anyway. Most regex
en are not that regular too.
12
Name:
Anonymous
2011-04-17 7:59
13
Name:
Anonymous
2011-04-17 11:12
Perl regexes can do pretty much anything, up to and including executing embedded Perl code, but unless you can adequately describe what you are trying to do, you'll just have to refer to the documentation.
14
Name:
Anonymous
2011-04-17 16:48
So I figured it out. All I had to do has use the /e regular-expression switch.
my $string = "Test test test <auto convert text> test test test.";
$string =~ s/<(auto convert text)>/'<a href="' . convert($1) . '">' . $1 . '<\/a>'/ge;
Such a simple solution, get your shit together, /prog/ .
Newer Posts