我想获得带有“管道转义”支持的管道分隔字符串的单个字符串,例如:
fielda|field b |field\|with\|pipe\|inside
Run Code Online (Sandbox Code Playgroud)
会让我:
array("fielda", "field b ", "field|with|pipe|inside")
Run Code Online (Sandbox Code Playgroud)
我将如何使用正则表达式达到该目标?
.*和之间的确切区别是[.]*什么?
我试图将这些放在括号内,用于反向引用,并看到结果不一样,虽然我不明白为什么.
本.应该匹配任何单个字符.
所以我猜它是否在方括号内是不应该对*(匹配零或更多)运算符很重要.但它是.为什么?
https://regex101.com/r/sB9wW6/1
(?:(?<=\s)|^)@(\S+) <-正向后看的问题
在prod:上像这样工作(?:\s|^)@(\S+),但是我需要一个正确的开始索引(没有空间)。
在JS中:
var regex = new RegExp(/(?:(?<=\s)|^)@(\S+)/g);
Run Code Online (Sandbox Code Playgroud)
解析正则表达式时出错:无效的正则表达式:/(?:(?<= \ s)| ^)@(\ S +)/
我究竟做错了什么?
更新
好吧,在JS中没有后顾之忧
但是无论如何,我需要一个正则表达式来获取比赛的正确开始和结束索引。没有领先的空间。
我正在尝试使用带有负向后看的正则表达式模式来验证电子邮件地址。更具体地说,只允许那些不以特定序列结尾的对象@mydomain.de。
这对我的大多数测试字符串都适用。但是,在字符串(\r\n)的末尾添加换行符似乎会破坏它,因为它不再匹配。
我知道通常可以使用来解决.endsWith()。我只是想在Javax模式注释中使用正则表达式。
Pattern p = Pattern.compile("^.*(?<!@mydomain\\.de)$")
p.matcher("test@gmail.com").matches() // => true
p.matcher("test@gmail.com\r\n").matches() // => false
Run Code Online (Sandbox Code Playgroud)
我希望两个字符串都匹配,因为它们不会以禁止序列结尾 @mydomain.de
我正在为具有关键字和逗号(分隔)/分号(EOL)分隔值的文件编写语法检查器(在 Java 中)。两个完整结构之间的空间量未指定。
需要什么:
在多行文件中查找任何重复的单词(连续和非连续)。
// Example_1 (duplicate 'test'):
item1 , test, item3 ;
item4,item5;
test , item6;
// Example_2 (duplicate 'test'):
item1 , test, test ;
item2,item3;
Run Code Online (Sandbox Code Playgroud)
我尝试应用该(\w+)(s*\W\s*\w*)*\1模式,但该模式无法正确捕获重复项。
我喜欢正则表达式。但是,我刚刚发现s在浏览器中运行JavaScript RegExp时无法使用该标志。我很好奇为什么不包括该标志?这真的很有帮助。
我已经看到有一个外部库XRegExp,它启用了此s标志(以及其他一些标志),但是我也很好奇为什么这些额外的(且有用的)标志在标准JavaScript中也不存在。我也不愿意包含另一个外部库...
这是一个示例,其中我尝试解决检测可能包含换行符的WordPress短代码的打开/关闭标签的问题(或者我必须在两者之间插入换行符以改善检测)。
//
// Let's take some input text, e.g. WordPress shortcodes
//
var exampleText = '[buttongroup class="vertical"][button content="Example 1" class="btn-default"][/button][button class="btn-primary"]Example 2[/button][/buttongroup]'
//
// Now let's say I want to extract the shortcodes and its attributes
// keeping in mind shortcodes can or cannot have closing tags too
//
// Shortcodes which have content between the open/closing tags can contain
// newlines. One of the issues with the flags is that …Run Code Online (Sandbox Code Playgroud)