[英]How to match regex with starting index in the middle of a string?
I'm writing a parser and I'd like to avoid chopping up the input string for performance reasons. 我正在编写一个解析器,并且我想避免由于性能原因而切分输入字符串。 Thus, I've created a
Stream
object that represents the string with a cursor: 因此,我创建了一个
Stream
对象,该对象用光标表示字符串:
const Stream = (string, cursor) => Object.freeze({
string,
cursor,
length: string.length - cursor,
slice: (start, end) => string.slice(start + cursor, end ? start + end : undefined),
move: distance => stream(string, cursor + distance),
})
I want to be able to use regular expressions to match against this string. 我希望能够使用正则表达式来匹配此字符串。 However, I don't care about anything in before the cursor.
但是,我不在乎光标之前的任何内容。 So suppose I have the following string and cursor:
因此,假设我有以下字符串和游标:
> string = 'hello ABCD'
'ABCD'
> cursor = 6
6
So we don't care about anything before the A
, but we want to be able to use regex to match all those uppercase letters: 因此我们不在乎
A
之前的任何内容,但我们希望能够使用正则表达式来匹配所有这些大写字母:
> re = /^[A-Z]+/
/^[A-Z]+/
I'm not sure how to get this to work. 我不确定如何使它正常工作。 I noticed when you use the
g
flag, then you can use RegExp.exec
and it will keep track of a lastIndex
property. 我注意到使用
g
标志时,可以使用RegExp.exec
,它将跟踪lastIndex
属性。 But then the ^
match will not start at lastIndex
... 但是,
^
匹配不会从lastIndex
开始...
Any ideas how I can get this to work efficiently? 有什么想法可以使我有效地工作吗? If I have to use a 3rd party regex library, I'm fine with that, but ideally this could be done with the native RegExp...
如果我必须使用第三方正则表达式库,那很好,但是理想情况下可以使用本机RegExp完成...
I would do with sed: 我会用sed做的:
sed -rn 's/^.{'$cursor'}([A-Z]+)$/\1/p'
where $cursor is a shell variable containing the number of ignored chars at the beginning. 其中$ cursor是一个shell变量,在开头包含被忽略的字符数。
Option -r
is extended regexp, -n
is do not print always, p
is print if match. 选项
-r
是扩展的regexp, -n
是不总是打印,如果匹配则p
是打印。
Now the question is how to port that to your language. 现在的问题是如何将其移植到您的语言。 Here you have some hints of how to use variables in regular expressions in Javascript.
在这里,您有一些关于如何在Java正则表达式中使用变量的提示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.