找回密码
 注册
Simdroid-非首页
楼主: qibbxxt

【讨论】正则系列

[复制链接]
发表于 2013-7-7 12:06:40 | 显示全部楼层 来自 新疆乌鲁木齐
本帖最后由 liuyalong008 于 2013-7-7 13:26 编辑

非贪婪搜索勉强应该也算个思路:
  1. regexprep(a,'^ *(.*?) *$','$1')
复制代码
不过我没有完全测试所有算例,写笔记写得头昏脑涨,我就不测了,假定它成功吧,呵呵...
ps:又是一堆美元符号,真心是讨厌!
用TeX语法写一个,可是"^"又是错的,用了转义字符也不行。干脆各位前半部分见代码、后半部分见TeX公式表达式吧:

点评

精彩!完全通过验证,马兄不会是会计出身吧,按$符号手都抽了  发表于 2013-7-7 12:13
回复 不支持

使用道具 举报

发表于 2013-7-7 12:19:34 | 显示全部楼层 来自 北京
Simdroid开发平台
bainhome 发表于 2013-7-5 20:18
cody原链接参看这里。
剔除且只剔除字符串首尾的多余空格(不包括其他符号)
题目原文:测试算例1:测试算例2 ...

再来一个非正则的,看别人的,觉得思路很好
strrep(strtrim(strrep(a,9,1000)),1000,9)
把制表符用一个较大的ascii来代替,然后用strtrim来去掉两端的白色字符
然后再替换回来

PS: deblank是去掉结尾的白色字符
        strtrim是去掉两端的
        要去掉前段的就strjust配合一下

点评

第10题已经在43楼附件中做了更新,后面就等着你们继续讨论,我的任务正式完成。  发表于 2013-7-7 14:33
回复 不支持

使用道具 举报

发表于 2013-7-7 17:52:54 | 显示全部楼层 来自 英国
我的解和lin兄的一样。

严格来讲,这题并不只是排除\t。原题要求的是处理ascii中的32, 故而\t \n \v \f \r都是无关的。所以用[^ ]应该是最好的办法。

评分

1

查看全部评分

回复 不支持

使用道具 举报

发表于 2013-7-8 10:26:23 | 显示全部楼层 来自 新疆乌鲁木齐
嗯.实际采用“' '”这种形式都差不多.毕竟test suite只给了\t一种,严格修整liu老弟的代码变成:
  1. function ans = removeSpaces(a)
  2. regexprep(regexprep(a,'^(?=[^\t\f\n\r\v])\s*',''),'(?=[^\t\f\n\r\v])\s*$','');
  3. end
复制代码
估计可以,没测试全部,随意改test5测了一下:
  1. a = sprintf('\f\n\vtab in front, space and tab both at end\r    ')
  2. a =

  3. tab in front, space and tab both at end
  4.    
  5. >> removeSpaces(a)
  6. ans =

  7. tab in front, space and tab both at end
复制代码

点评

效率肯定惨不忍睹,不过出题目的是多解扩展思路,这点自己就不想深究了,呵呵。  发表于 2013-7-8 10:28
回复 不支持

使用道具 举报

发表于 2013-7-22 18:17:29 | 显示全部楼层 来自 英国

题目11:倒读隐语

把每个单词的第一个字母移至末尾,并在其后加上字母ay。
原题链接见:Problem 1721. Backslang, odds are you used it at some point in time...

题目原文:
  1. Hatstay tiay! Onay oremay onay esslay. Ellway erehay reaay omesay xampleseay:

  2. str = 'The sky is falling, the sky is falling, or is it?'

  3. output = Hetay kysay siay allingfay, hetay kysay siay allingfay, roay siay tiay?

  4. Ustjay aay otenay, omesay unctuationpay ndaay apitalscay oday ountcay.

  5. Oodgay Ucklay!
复制代码
测试题目:


  1. %%
  2. str = 'The sky is falling, the sky is falling, or is it?'
  3. output = 'Hetay kysay siay allingfay, hetay kysay siay allingfay, roay siay tiay?'
  4. assert(isequal(backslang(str),output))

复制代码
  1. %%
  2. str = 'If Allen is Janes husband and Tom is Jill husband, who is Roys wife?'
  3. output = 'Fiay Llenaay siay Anesjay usbandhay ndaay Omtay siay Illjay usbandhay, howay siay Oysray ifeway?'
  4. assert(isequal(backslang(str),output))
复制代码
  1. %%
  2. str = 'This is the sentence I will use.'
  3. output = 'Histay siay hetay entencesay Iay illway seuay.'
  4. assert(isequal(backslang(str),output))
复制代码
  1. %%
  2. str = 'Christopher Columbus sailed the ocean blue!'
  3. output = 'Hristophercay Olumbuscay ailedsay hetay ceanoay luebay!'
  4. assert(isequal(backslang(str),output))
复制代码
回复 不支持

使用道具 举报

发表于 2013-7-23 08:25:48 | 显示全部楼层 来自 北京
nwcwww 发表于 2013-7-22 18:17
把每个单词的第一个字母移至末尾,并在其后加上字母ay。
原题链接见:Problem 1721. Backslang, odds are y ...

我先贴一个:
  1. regexprep(str,{'(?<=[A-Z])(\w)','(\<\w)(\w+\>)','(\<[A-Z]\>)'},...
  2.       {'${upper($1)}','$2${lower($1)}ay','$1ay'})
复制代码
首先把首字母是大写的单词中的第二个字符转成大写
其次把token 1和token 2调换位置,并加上ay
最后再把单个字符大写的保留并加上ay
个人觉得写得还是挺啰嗦的

评分

1

查看全部评分

回复 不支持

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2026-1-12 15:33 , Processed in 0.033886 second(s), 15 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表