2 shunfurh shunfurh 于 2017.09.07 11:08 提问

Advanced Regular Expression

Given a paragraph and several regular expressions, your task is to find the first match of each regular expression. If multiple matches exist, you must output the longest one.
The regular expression can contain some meta-characters described below:

  • Matches the preceding character/meta-character zero or more times.
  • Matches the preceding character/meta-character one or more times. ? Matches the preceding character/meta-character zero or one time. . Matches any single character. [xyz] A character set. Matches any characters within the brackets. [^xyz] A negative character set. Matches any characters NOT within brackets. \d Matches a digit character. \D Matches a non-digit character. \a Matches a alpha character. \A Matches a non-alpha character. \n Matches a new-line character. (ASCII 10) \s Matches any white-space character including space, tab and new-line. \S Matches any non-white-space character. \t Matches a tab character. (ASCII 9) \w Matches an alphanumeric character (including "_") \W Matches a non-alphanumeric character. * Matches "*". + Matches "+". \? Matches "?". . Matches ".". [ Matches "[". ] Matches "]". \ Matches "\".

Note:
1. [ ] always appear in pairs and will not be nested.
2. The character set within [ ] will not be empty.
3. Meta-characters can not be used within [ ].

Input

The first line of input contains an integer n, the number of test cases. Each test case begins with an integer L, and the following L lines give the paragraph. The next line contains an integer m, with the following m lines containing a regular expression each.

You can assume that each paragraph contains no more than 100,000 characters and each regular expression has no more than 100 characters.

Output

For each test case, first print the case number on a single line, then print the match for each regular expression. If no match is found, print "No match is found". Print a blank line after each test case.

Always print a '\n' after each match unless the match ends with a '\n' itself. Print a line with 44 dashes after each match.

Empty match is NOT valid.

Some Examples:
m.n matches "man", "men", "min" but not "moon".
Te+st matches "test", "teest", "teeeest" etc. BUT NOT "tst".
Te*st matches "test", "teest", "teeeest" etc. AND "tst".
[aeiou] matches every lowercase vowel.
[,.?] matches a literal ",", "." or "?".
[^^] matches any character except "^".

Sample Input

2
1
Hello world!
2
He.*d!
He*d!
1
Sunny Cup
1
Sunny Cup

Sample Output

Case #1:

Hello world!

No match is found

Case #2:

Sunny Cup

1个回答

caozhy
caozhy   Ds   Rxr 2017.09.22 08:34
已采纳
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
Advanced Regular Expression Tips and Techniques
Learn http://pypix.com/tools-and-tips/advanced-regular-expression-tips-techniques/
Regular Expression
Regular Expression
Regular Expression Matching -- LeetCode
原题链接: http://oj.leetcode.com/problems/regular-expression-matching/  这个题目比较常见,但是难度还是比较大的。我们先来看看brute force怎么解决。基本思路就是先看字符串s和p的从i和j开始的子串是否匹配,用递归的方法直到串的最后,最后回溯回来得到结果。假设现在走到s的i位置,p的j位置,情况分为下列两种:  (1)p[j
regular expression
regular expression is the main content
Invalid Regular Expression Options
出现这个问题的时候,js文件本身并没有出现什么错误,可是尝试以下方法解决问题, 选中出错的js文件,右键选择MyEclipse->Exclude from Validation, 然后在右键选择Myeclipse->Run Validation,看看能否解决问题 这是我在网上搜到的一个方法,反正是我的问题解决了~~
sed识别regular expression
小弟在sed使用中,对正则表达式有几个地方犯迷糊,为了彻底弄明白,试验之…… 特将结果呈与各位,请师兄们批评指正!也让初学者少走弯路。虽说有些问题在明白之后,感觉很简单,贴出来好像没必要;但在明白之前,的确很让人糊涂,所以还是决定贴出来。 In basic regular expressions the metacharacters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions /?
浅谈正则表达式(Regular Expression)
一、什么是正则表达式? 简单的说:正则表达式(Regular Expression)是一种处理字符串匹配的语言; 正则表达式描述了一种字符串匹配的模式,可以用来检查一个字符串是否含有某种子串,对匹配到的子串进行“取出”或“替换”操作。 二、正则表达式的应用 正则表达式在实际的开发过程中非常实用,能够快速解决一些复杂的字符串处理问题,下面我对正则表达式的应用做一些简单分类:
Java正则表达式(Java Regular Expression)
也不知道是谁首先将Regular Expression翻译成了"正则表达式"这么一个文诌诌的名词。无论是按字面还是按其用途,Regular Expression都应该是规则表达式,指对特定字符串构成规则/特征的描述。这种构成规则,也就是模式(Pattern)。       最早应用在Unix系统中的正则表达式尽管没有一个统一的权威标准,但各种正则表达式可说是大同、小异。     Java语言在
正则表达式 Regular Expression
重新整理记录一下正则规则 定义 Regular Expression - 正规的/有规律的表达式,是计算机科学的一个概念。使用一个字符串来描述、匹配一系列符合某个句法规则的字符串 最初这个概念是由Unix中的工具软件如sed和grep普及开的,通常简写为regex 几乎所有操作系统以及编程语言中都有正则表达式的应用 引擎 正则引擎主要分为两大类: DFA NFA P
正则表达式基础 Regular Expression
正则表达式基础 Regular Expression 正则表达式简介   n  为什么需要正则表达式? q  文本的复杂处理。 n  正则表达式的优势和用途? q  一种强大而灵活的文本处理工具; q  提供了一种紧凑的、动态的方式,能够以一种完全通用的方式来解决各种字符串处理(例如:验证、查找、替换等)问题; q  大部分语言[d1] 、数据库都支持正则表达式。 n  正则表达