Questions tagged «regex»

正则表达式提供了一种声明性语言来匹配字符串中的模式。它们通常用于字符串验证,解析和转换。由于正则表达式尚未完全标准化,因此带有此标签的所有问题还应包括一个标签,用于指定适用的编程语言或工具。注意:询问HTML,JSON等正则表达式往往会产生负面反应。如果有解析器,请改用它。

1
grunt(minimatch / glob)文件夹排除
我遇到一种情况,我试图使用grunt来清理代码库,但不包括特定文件夹。 grunt在引擎盖下使用minimatch(类似于bsdglob)来匹配文件,但是我似乎无法弄清楚如何排除文件夹的.gitignore样式。 我想摄取这个: ignoreme 并匹配这些: /folder/path/here/to/something/ok.js /another/folder/path.js /test.js 但不符合以下条件: /folder/ignoreme/something.js /folder/path/here/to/ignoreme/metoo/file.js 这将匹配所有内容,包括ignoreme: /**/*.js 所以我想我可以做些类似的事情: /**/!(ignoreme)/**/*.js 但这与ignoreme文件夹中的文件匹配。 我已经习惯了正则表达式,但是似乎无法在这里弄清楚如何重复一个模式或类似的东西-我也尝试过类似的东西: /(!(ignoreme)|*)*/*.js 希望容器会重复,但是那行不通,只是无法匹配所有内容。 有什么方法可以通过正则表达式来查找文件路径,还是可以为我工作? 更新: 这是我目前处理此问题的方式: var pattern = /\/ignoreme\// var files = grunt.file.expandFiles(arrayOfFilesPassedToMinimatch).filter(function(f){ return !pattern.test(f) }) 如果在minimatch中可能排除文件夹,我仍然会感兴趣。



5
文本处理-Python vs Perl性能[关闭]
这个问题不太可能帮助将来的访客。它仅与较小的地理区域,特定的时间段或极为狭窄的情况(通常不适用于Internet的全球受众)有关。要获得使该问题更广泛适用的帮助,请访问帮助中心。 8年前关闭。 这是我的Perl和Python脚本,可以对大约21个日志文件进行一些简单的文本处理,每个日志文件大约300 KB到1 MB(最大)x重复5次(总共125个文件,由于日志重复了5次)。 Python代码(修改为使用已编译re和using的代码re.I) #!/usr/bin/python import re import fileinput exists_re = re.compile(r'^(.*?) INFO.*Such a record already exists', re.I) location_re = re.compile(r'^AwbLocation (.*?) insert into', re.I) for line in fileinput.input(): fn = fileinput.filename() currline = line.rstrip() mprev = exists_re.search(currline) if(mprev): xlogtime = mprev.group(1) mcurr = location_re.search(currline) if(mcurr): print fn, …


5
正则表达式错误-无需重复
使用此表达式时出现错误消息: re.sub(r"([^\s\w])(\s*\1)+","\\1","...") 我在RegExr中检查了正则表达式,它.按预期返回。但是当我在Python中尝试时,出现以下错误消息: raise error, v # invalid expression sre_constants.error: nothing to repeat 有人可以解释一下吗?
74 python  regex 


10
python中已编译的regex对象的类型
python中已编译的正则表达式的类型是什么? 我特别要评估 isinstance(re.compile(''), ???) 确实是出于自省的目的。 我的一个解决方案是,具有一些全局常量REGEX_TYPE = type(re.compile('')),但是它看起来并不优雅。 编辑:我想这样做的原因是因为我有字符串列表和已编译的正则表达式对象。我想根据列表“匹配”字符串 对于列表中的每个字符串,请尝试检查字符串是否相等。 对于列表中的每个正则表达式,请尝试检查字符串是否与给定的模式匹配。 我想到的代码是: for allowed in alloweds: if isinstance(allowed, basestring) and allowed == input: ignored = False break elif isinstance(allowed, REGEX_TYPE) and allowed.match(input): ignored = False break
74 python  regex  types 

11
如何验证正则表达式?
我想在PHP中测试正则表达式的有效性,最好在使用前进行测试。这样做的唯一方法是实际尝试apreg_match()并查看其是否返回FALSE吗? 有没有更简单/正确的方法来测试有效的正则表达式?
74 php  regex 

4
grep --ignore-case --only
同时使用--ignore-case和--only-match选项时,grep失败。例: $ echo "abc" | grep -io abc abc $ echo "ABC" | grep -io abc $ 但 $ echo "abc" | grep -i abc abc $ echo "ABC" | grep -i abc ABC 根据手册页: -o, --only-matching Show only the part of a matching line that matches PATTERN. -i, --ignore-case …
74 regex  bash  unix  grep  gnu 

5
如何使用grep在`name =`之后获取任何内容?
我一直在尝试grep之后的任何内容,仅name=包含空格和字母数字。 例如: name=some value here 我懂了 some value here 我对此完全陌生,以下grep匹配包括在内的所有内容name=。 grep 'name=.*' filename 任何帮助深表感谢。
74 regex  bash  grep 

6
如何获得字符串中的第一个单词
文字为: WYATT - Ranked # 855 with 0.006 % XAVIER - Ranked # 587 with 0.013 % YONG - Ranked # 921 with 0.006 % YOUNG - Ranked # 807 with 0.007 % 我只想得到 WYATT XAVIER YONG YOUNG 我试过了 : (.*)?[ ] 但这给了我: WYATT - Ranked
73 python  regex 

5
使用正则表达式的Scala捕获组
假设我有以下代码: val string = "one493two483three" val pattern = """two(\d+)three""".r pattern.findAllIn(string).foreach(println) 我希望findAllIn只返回483,但是它返回了two483three。我知道我只能unapply提取那一部分,但是我必须为整个字符串设置一个模式,例如: val pattern = """one.*two(\d+)three""".r val pattern(aMatch) = string println(aMatch) // prints 483 是否有另一种方法可以实现,而不必java.util直接使用类,也可以不使用unapply?


5
如何在Java中从输入文本中删除标点符号?
我试图使用Java中用户的输入来获取一个句子,并且我需要使其小写并删除所有标点符号。这是我的代码: String[] words = instring.split("\\s+"); for (int i = 0; i < words.length; i++) { words[i] = words[i].toLowerCase(); } String[] wordsout = new String[50]; Arrays.fill(wordsout,""); int e = 0; for (int i = 0; i < words.length; i++) { if (words[i] != "") { wordsout[e] = words[e]; wordsout[e] = wordsout[e].replaceAll(" …

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.