正则表达式帮助 -你好,我需要一些帮助来使用正则表达式函数--
字符串就像——
“很好。2020。可选。(不)”
必填——“Fine_2020_Optional”
我尝试了几种组合,但无法在一个正则表达式中获得结果。 我在 for 循环脚本中使用这种类型的正则表达式。
有什么帮助吗?
谢谢。
回复:正则表达式帮助 -谢谢。 这非常有帮助回复:正则表达式帮助 -@Craige_Hales工藤 x 10^6
谢谢
回复:正则表达式帮助 -你好@Byron_JMP !
我可能会这样做:
source = "(aaa).Fine.2020.Optional.(Not).more.(leave out).last.(bbb)";
//
// remove text between parens, change . to _
//
parts = {}; // accumulator list of the individual parts
ipart = 0; // index to the parts list
rc = Pat Match( // test the rc (below) to make sure the match worked
source,
patpos(0) // PatRepeat must start at the beginning.
+
Pat Repeat(
( "." | Pat Pos( 0 ) ) // advance past a required period, or begin of string
+ // followed by either...
(
( "(" + Pat Break( ")" ) + ")" )
// above: match open paren and stop on a close paren. Parens and everything
// between is rejected by not adding anything to the list of parts.
| // or
( ( Pat Break( "." ) | Pat Rem() ) >> parts[ipart += 1] )
// above: scan forward up to, but not including, a period, or if
// that doesn't work, to the end (remainder) of the string.
// ipart += 1 adds 1 before subscripting. It is OK to add an item
// to a list just beyond the end of the list. The >> operator stores
// the matched value on its left into the variable on its right.
)
)
+
patrpos(0) // PatRepeat must stop at the end.
);
// re-join the parts with the _ separator
result = if(rc,Concat Items( parts, "_" ),"problem with match");
show(result);结果=“Fine_2020_Optional_more_last”;
它相当长,而且相当不透明(也许不像正则表达式那么不透明)。 但...
- Regex() 的真正目标是进行多次替换; GLOBALREPLACE 是高效完成的。 您可能需要第二个正则表达式来删除 (文本),如果有多个,也可以使用 GLOBALREPLACE。
- Word(),在 Jeff 的例子中也不错如果您知道要保留多少个以及(文本)出现的位置。 它的简单性和可维护性以及速度是无与伦比的。
- PatMatch() 在其他方面大放异彩;它实际上是用于提取信息,或者最多是在单个位置中修改字符串(没有 GLOBALREPLACE)。 该示例显示 PatMatch() 提取信息以生成零件列表,然后重新组装零件。
另一方面,这是关于如何使用模式匹配从字符串中提取信息的精彩介绍。 真正的好处来自于可以在单个匹配中解析较长的字符串(如上所述,在短字符串上),因为匹配仅具有一次启动开销。 通过让 PatRepeat 一次性遍历字符串,并且不修改字符串,匹配速度可以相当快。
回复:正则表达式帮助 -模式匹配也可能是一个很好的解决方案,但我总是被语法绊倒。 要是周围有人能说流利的 snbol 就好了…… @Craige_Hales
回复:正则表达式帮助 -不要忘记用于进行字符串操作的内置 JMP 函数,例如代替() ,芒格()和,我最喜欢的,单词() 。 对于这样的事情,这些语法可能更容易处理。
str="Fine.2020.Optional.(Not)";
show(substitute(str, ".", "_"));
show(word(1, str, ".") || "_" || word(2, str, ".") || "_" || word(3, str, "."));
show(concat items(words(str, ".")[1::3], "_"));
日志:
Substitute(str, ".", "_") = "Fine_2020_Optional_(Not)";
Word(1, str, ".") || "_" || Word(2, str, ".") || "_" || Word(3, str, ".") = "Fine_2020_Optional";
Concat Items(Words(str, ".")[Index(1, 3)], "_") = "Fine_2020_Optional";
回复:正则表达式帮助 -好的,所以离线时我收到了一些关于这个非常简短的解决方案的评论。
以下是如何自行到达那里。 这可能是作弊,但这要容易得多。
我制作了一个表格,其中包含示例。 选择该列并使用重新编码。
在 Recode 中,我使用了替换字符串,并打开了正则表达式。
在重新编码对话框中,我打开了两个脚本选项(右下角)
然后将脚本保存到脚本窗口,复制一点并粘贴到这里。 (好吧,验证它有效然后粘贴它。)
非常感谢@欧内斯特帕索尔


哦,还有一点使用“\”。而不是 ”。”独自的。 反斜杠是转义字符,不会用“_”替换所有内容。 这是完成这项工作所需的唯一一点正则表达式知识。
回复:正则表达式帮助 -正则表达式( “这.is.jmp” , “\。” , “_” 、全局替换);
回复:正则表达式帮助 -这确实非常有帮助。 谢谢。
回复:正则表达式帮助 -该网站提供了一些有关正则表达式的重要一般信息:
https://www.regular-expressions.info/quickstart.html
还有一个工具可以根据您的输入和预期输出生成正则表达式:
http://regex.inginf.units.it/
回复:正则表达式帮助 -谢谢保罗,
很少有更简单的,我能做到,但就是这个。这给我带来了一些麻烦。 下次如果我发现任何问题,我会向您提供更多详细信息。 您是否有一些关于正则表达式的参考/链接可以与我/我们分享并提供很好的示例? 这将非常有帮助。
再次感谢。回复:正则表达式帮助 -如果我们有更多输入和预期输出以及您遇到问题的示例,可能会有所帮助。 下面的 JSL 适用于您的单一案例,但我不确定它是否适用于您的所有数据。
Regex("Fine.2020.Optional.(Not)",
"(\w+?)\.(\d+?)\.(\w+?)\.\(\w+?\)",
"\1_\2_\3"
);