正则表达式相当于 str.getSubstring(x, y);
从位置 x
到位置 y
的子字符串的 RegExp 等价物是什么?
例如:
0001..STACK.OVERFLOW...IS.AWESOME.13052011
我知道这个固定长度字符串中名为 'status'
的字段的值是从位置 26 开始的 8 个字符。JavaScript(和 Java)中的正则表达式会是什么样子来获取“AWESOME”
字符串?
我正在尝试构建一个大型机屏幕解析器,该解析器通过 JMS 作为固定长度字符串。我们的想法是编写一个 UI,用户可以在其中突出显示字符串的一部分、填充“字段名称”字段、选择类型(int、String..)并自动生成 Java 类。
What would be a RegExp equivalent of substring from position x
to position y
?
For example:
0001..STACK.OVERFLOW...IS.AWESOME.13052011
I know that the value of a field called 'status'
in this fixed length string is 8 characters starting from position 26. What would a regex in JavaScript (and Java) look like to get the "AWESOME"
string?
I'm trying to build a parser of mainframe screens that come over JMS as fixed length strings. And the idea is to write a UI where a user could highlight a section of a string, fill the 'field name' field, select type (int, String..) and have a Java class generated automatically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
第一个 .{26} 会吃掉前 26 个字符,无论它们是什么。 (.{8}) 捕获接下来的 8 个字符并存储它们。对于 Javascript,您可以使用
matches[1] 将是您要查找的子字符串。 (matches[0] 始终包含整个匹配字符串)
请注意,如果需要,可以用字符类替换 .s(例如 [\w]{26}[\w]{8})
The first .{26} eats the first 26 characters no matter what they are. The (.{8}) captures the next 8 characters and stores them. For Javascript you can use
and matches[1] will be the substr that you're looking for. (matches[0] always contains the entire matched string)
note that the .s can be replaced by character classes if you want (ex. [\w]{26}[\w]{8})
在 Java 中,
Pattern.compile("(?s)(?<=.{26}).{8}")
应该可以做到这一点,子字符串是匹配的文本。您无法在 javascript 中执行此操作,因为 JavaScript 正则表达式不支持lookbehind,但您可以执行捕获组。
在 JavaScript 中,您可以获得的最接近的是
/^[\s\S]{26}([\s\S]{8})/
,并且子字符串位于组 1 中。请注意,这计算的是字符,而不是代码点,因此可能会分割代理对,但 JavaScript 和 Java 的内置
substring
函数也有同样的问题。In Java,
Pattern.compile("(?s)(?<=.{26}).{8}")
should do it, and the substring is the matched text.You can't do it in javascript because JavaScript regexs don't support lookbehind, but you can do a capturing group.
In JavaScript the closest you can get is
/^[\s\S]{26}([\s\S]{8})/
, and the substring is in group 1.Note, that this counts chars, not codepoints, so might split a surrogate pair, but JavaScript and Java's built in
substring
functions have the same problem.像
^(?<=.{2}).*(?=.{3})$
这样的东西会给你从 2 之后开始到 3 之前结束的子字符串。是的,这不适用于 JS,但即使可以,也要坚持使用子字符串。Something like
^(?<=.{2}).*(?=.{3})$
will give you substring starting after 2 and ending 3 position before. And yeah, this doesn't work with JS, but even if it did, stick with substring.我认为这应该可行:
这在 JavaScript 中无法工作,因为 JavaScript 不支持后向查找。
编辑(关于换行符):如果您希望
.
匹配新行,则必须在 Java 中指定Pattern.DOTALL
。有关更多详细信息,请查看模式,特别是Pattern.compile(String regex, int flags)
。您还可以通过在正则表达式中包含
(?s)
来打开此功能。另一种想法:如果您不想严格获取以 28 个字符开头的子字符串,而是想要获取遵循某种其他模式的子字符串(例如“STATUS”之后的 8 个字符),则可以这样做:
I think this should work:
This will not work in JavaScript because JavaScript does not support look-behind.
Edit (regarding newlines): if you want the
.
to match new lines, you have to specifyPattern.DOTALL
in Java. For more detail, look at Pattern in JavaDocs, particularlyPattern.compile(String regex, int flags)
.You can also turn this on by including
(?s)
in your regex.Another thought: if, instead of strictly getting the substring starting 28 characters in, you want to get one that follows some other pattern (like 8 characters after "STATUS"), you could just do this: