编程介的小学生 2017-02-22 14:40 采纳率: 20.5%
浏览 909
已采纳

Pattern Matching Using Regular Expression

A regular expression is a string which contains some normal characters and some meta characters. The meta characters include,
. means any character

[c1-c2] means any character between c1 and c2 (c1 and c2 are two characters)

[^c1-c2] means any character not between c1 and c2 (c1 and c2 are two characters)

  • means the character before it can occur any times

  • means the character before it can occur any times but at least one times

\ means any character follow should be treated as normal character
You are to write a program to find the leftmost substring of a given string, so that the substring can match a given regular expression. If there are many substrings of the given string can match the regular expression, and the left positions of these substrings are same, we prefer the longest one.

Input

Every two lines of the input is a pattern-matching problem. The first line is a regular expression, and the second line is the string to be matched. Any line will be no more than 80 character. A line with only an "end" will terminate the input.

Output

For each matching problem, you should give an answer in one line. This line contains the string to be matched, but the leftmost substring that can match the regular expression should be bracketed. If no substring matches the regular expression, print the input string.

Sample Input

.*
asdf
f.*d.
sefdfsde
[0-9]+
asd345dsf
[^*-*]
asdffasd
b[a-z]*r[s-u]*
abcdefghijklmnopqrstuvwxyz
[T-F]
dfkgjf
end

Sample Output

(asdf)
se(fdfsde)
asd(345)dsf
(a)sdffasd
a(bcdefghijklmnopqrstu)vwxyz
dfkgjf

  • 写回答

2条回答 默认 最新

  • threenewbee 2017-02-28 21:23
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝