我的表格中有文字:
Name=Value1
Name=Value2
Name=Value3
Run Code Online (Sandbox Code Playgroud)
使用Perl,我希望/Name=(.+?)/每次出现时都匹配并提取(.+?)并将其推送到数组上.我知道我可以$1用来获取我需要的文本,我可以=~用来执行正则表达式匹配,但我不知道如何获得所有匹配.
我之前曾在Groovy中询问过如何做到这一点.但是,由于所有CPAN库,现在我在Perl中重写我的应用程序.
如果页面包含以下链接:
<a href="http://www.google.com">Google</a> <a href="http://www.apple.com">Apple</a>
输出将是:
Google, http://www.google.com Apple, http://www.apple.com
在Perl中执行此操作的最佳方法是什么?
我被赋予了将一种语言"翻译"成另一种语言的工作.对于使用正则表达式的简单逐行方法,源代码太灵活(复杂).我在哪里可以了解有关词法分析和解析器的更多信息?
当你有正则表达式时,词法分析器很容易编写.今天我想用Python编写一个简单的通用分析器,并提出:
import re
import sys
class Token(object):
""" A simple Token structure.
Contains the token type, value and position.
"""
def __init__(self, type, val, pos):
self.type = type
self.val = val
self.pos = pos
def __str__(self):
return '%s(%s) at %s' % (self.type, self.val, self.pos)
class LexerError(Exception):
""" Lexer error exception.
pos:
Position in the input line where the error occurred.
"""
def __init__(self, pos):
self.pos = pos
class Lexer(object):
""" A simple regex-based lexer/tokenizer.
See below for an example of …Run Code Online (Sandbox Code Playgroud)