简体   繁体   English

正则表达式从后面匹配字符串

[英]Regex to match string from the back

Let's say we have a string "text\t1\nText that has to be extracted" what regex can be used so that we check the string from the back that is from the last " to n because the start of the string can change. In this case, I need to get only Text that has to be extracted . What generic regex can we use here?假设我们有一个字符串"text\t1\nText that has to be extracted"可以使用什么正则表达式,以便我们检查从最后一个"n的字符串,因为字符串的开头可以更改。在在这种情况下,我只需要获取Text that has to be extracted 。我们可以在这里使用什么通用正则表达式?

I used this (?<=\\n1\\n)(.*)(?=“) but this will not work if the pattern before n changes to n2 or ntext .我使用了这个(?<=\\n1\\n)(.*)(?=“)但如果n之前的模式更改为n2ntext ,这将不起作用。

Any help is appreciated.任何帮助表示赞赏。

You may use this regex:你可以使用这个正则表达式:

/(?<=\\n)[^"\\]+(?="$)/

RegEx Demo正则表达式演示

RegEx Details:正则表达式详细信息:

  • (?<=\\n) : Lookbehind to make sure we have a \n before the current position (?<=\\n) :向后看以确保我们在当前 position 之前有一个\n
  • [^"\\]+ : Match 1+ of any character that is not " and not \ [^"\\]+ : 匹配任何不是"和不是\的字符的 1+
  • (?="$) : Make sure we have a " before line end ahead (?="$) : 确保我们在行结束前有一个"

Can't you just split and take the last element?你不能只拆分并取最后一个元素吗?

 var item = "text\n1\nText that has to be extracted"; var last = item.split(/\n/g).reverse()[0]; console.log(last) // "Text that has to be extracted"

/^(\d+)\n([^\n"]+)"$/ may have some edge cases, but will find the number (one or more digits), followed by a newline, followed by any character that is neither newline nor a double quote, followed by a literal double quote. /^(\d+)\n([^\n"]+)"$/可能有一些边缘情况,但会找到数字(一位或多位),后跟换行符,后跟任何既不是换行符也不是双引号,后跟文字双引号。

This would require that the double quote occurs immediately before the end-of-line (EOL), but if that's not required (for example, if you have a semi-colon after the closing quote), remove $ from the end.这将要求双引号紧接在行尾 (EOL) 之前出现,但如果不需要(例如,如果在右引号后有分号),请从末尾删除$

Edit编辑

Just noticed that it's the literal text \n and not a newline character.刚刚注意到它是文字文本\n而不是换行符。

/(?<=\\n)(\d+)\\n((?:[^\\"]+|\\.)*)"/

Regex101 example正则表达式 101 示例

Breakdown:分解:

  • (?<=\\n) looks for a \ followed by the letter n . (?<=\\n)查找后跟字母n\
  • (\d+) captures the 1-or-more digits. (\d+)捕获 1 个或多个数字。
  • \\n matches a literal \ followed by the letter n . \\n匹配文字\后跟字母n
  • (...*) matches some text that repeats 0 or more times. (...*)匹配一些重复 0 次或更多次的文本。
  • (?:...|...) matches any character that are neither a literal \ character nor a double quote character... OR a literal \ character that is followed by "anything" so you can have \n or \" etc. The entire group is matched repeatedly. (?:...|...)匹配既不是文字\字符也不是双引号字符的任何字符...或后面跟着 "anything" 的文字\字符,所以你可以有\n\"等等。整个组被重复匹配。
  • " at the end ensures that you're inside (well, we hope) a double-quoted string on the same line. "最后确保您在同一行的双引号字符串中(好吧,我们希望如此)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM