Regular Expression_1: Pattern

Regular Expression

Is it difficult? I think it is ture before I use it. :)

First thing, we must see that Regular Expression syntax definition is not unique, even I can say lots of definition here.

So if you study RE from some reference and use it in another environment, you will get nothing.

Fortunately, they are same concept.

Here I introduced base on javascript regular expression.

[Concepts]
* Regular Expression

From http://en.wikipedia.org/wiki/Regular_expression

In computing, a regular expression (abbreviated as regexp or regex, with plural forms regexps, regexes, or regexen) is a string that describes or matches a set of strings, according to certain syntax rules. Regular expressions are used by many text editors and utilities to search and manipulate bodies of text based on certain patterns. Many programming languages support regular expressions for string manipulation. For example, Perl and Tcl have a powerful regular expression engine built directly into their syntax. The set of utilities (including the editor ed and the filter grep) provided by Unix distributions were the first to popularize the concept of regular expressions.

Many modern computing systems provide wildcard characters in matching filenames from a file system. This is a core capability of many command-line shells and is known as globbing. Wildcards differ from regular expressions in that they can only express very restrictive forms of alternation.

[Sample 1:]

Code:

this.form1.txtRE.value = "^(//S*)@(//w+(?://.//w+)+)$";
this.form1.txtText.value = "youraccount@company.com";

function btnMatch_onclick() {
var sRegE = this.form1.txtRE.value;
var sText = this.form1.txtText.value;

try
{
var match = sText.match(sRegE);
if (null == match)
{
alert("no found.");
}
else
{
var sMsg = "match text:/n"
sMsg += sText.substring(match.index, match.lastIndex);

sMsg += "/nmatch captures:/n";
for (var i = 0; i < match.length; i++)
{
sMsg += "[" + i + "] " + match[i] + "/n";
}

alert(sMsg);
}
}
catch ( e )
{
alert("error!");
}
}

You will get result:

---------------------------
Windows Internet Explorer
---------------------------
match text:
youraccount@company.com
match captures:
[0] youraccount@company.com
[1] youraccount
[2] company.com

---------------------------
OK
---------------------------
Explain:

1.1 The code does three things: validate input; match it; and parse it to account name and site name.

1.2 We explain parse functions, if match is OK,

match[0] is whole matched text.

match[1] is text in first pair brackets.

match[2] is text in second pair brackets.

[3],[4]... if there are third, forth pair brackets.

Ok, we look the RE again,

"^(//S*)@(//w+(?://.//w+)+)$"

There are three bracket, in RE call it as Pattern or subExpresion,

why lost one?

reason is the third one pattern has '?:', it hints only match don't capture.
there are 4 kind pattern syntax:

(pattern): match and capture

(?:pattern): match and don't capture

(?=pattern): lookahead, match and don't capture, if equal, match.

lookahead do not comsume characters, for example:

windows(?95|98|NT|2000), matches "windows" in "windows2000", after match, match's last index is point to "s".

(?!pattern): lookahead, match and don't capture, if not equal, match.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里是完整的代码,包括 isMatch 函数的实现: ```python import re def isMatch(text, pattern): """Simple regular expression @param text: text string @param pattern: pattern string @return True: if pattern matches text False: otherwise """ # 使用 re 模块的 search 函数进行匹配 match = re.search(pattern, text) # 如果找到了匹配项,则返回 True if match: return True # 如果没有找到匹配项,则返回 False else: return False def main(): tests = [('abcdefg', '.*g', True), ('abcdefg', 'abcdefg', True), ('abcdefg', 'abcc*defg', True), ('abcdefg', 'a.*', True), ('aaaaaaa', 'a*', True), ('abcdefg', 'abcdefg*', True), ('abcdefg', 'abc.efg', True), ('abcdefg', 'abcdef.g', False), ('abcdefg', 'abcdefg.', False), ] print("Check your answers below.") for text, pattern, ans in tests: match = isMatch(text, pattern) print("'{}' matches '{}'? \tyour answer: {} <-> right answer: {}".format(text, pattern, match, ans)) if __name__ == '__main__': main() ``` 这里我们使用了 Python 标准库中的 re 模块,其中的 search 函数可以用于在文本中查找与模式匹配的字符串。我们将 isMatch 函数的实现简单地封装在 re.search 函数的返回值上,如果找到了匹配项,则返回 True,否则返回 False。 在 main 函数中,我们定义了 tests 列表,包含了要检查的多组 text 和 pattern,以及正确的答案。通过遍历 tests,调用 isMatch 函数检查每个元组的 text 和 pattern 是否匹配,并将结果与正确答案进行比较,输出检查结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值