匹配简单分隔符,但不匹配分隔符本身 [英] Match between simple delimiters, but not delimiters themselves

查看:73
本文介绍了匹配简单分隔符,但不匹配分隔符本身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看仅在文本文件中的 JSON 数据.除了使用正则表达式获取引号之间的值之外,我不想做任何事情.我只是将它用作帮助练习正则表达式的一种方式,并且到了这一点,这似乎应该很简单,但事实证明并非如此(至少对我和办公室的其他一些人而言).我已经在正则表达式中轻松匹配了复杂的 url,所以我对正则表达式并不完全陌生.这对我来说似乎是一个奇怪的案例.

I was looking at JSON data that was just in a text file. I don't want to do anything aside from just use regex to get the values in between quotes. I'm just using this as a way to help practice regex and got to this point that seems like it should be simple, but it turns out it's not (at least to me and a few other people at the office). I've matched complicated urls with ease in regex so I'm not completely new to regex. This just seems like a weird case for me.

我试过了:

/(?:")(.*?)(?:")/

/"(.*?)"/

还有其他几个,但这些让我最接近.

and several others but these got me the closest.

基本上我们可以忘记它是 JSON 而只是说我想匹配单词 value 和value"和stuff"中的东西.我尝试的所有内容都包含引号,因此我必须在分隔符之后清理字符串,否则字符串实际上是带有引号的值".

Basically we can forget that it's JSON and just say I want to match the words value and stuff out of "value" and "stuff". Everything I try includes the quotes, so I'd have to clean the strings afterwards of the delimiters or else the string is literally "value" with the quotes.

任何帮助将不胜感激,无论是简单还是复杂,我都很想知道!谢谢

Any help would be much appreciated, whether this is simple or complicated, I'd love to know! Thanks

更新:好的,所以我想我会使用 (?<=")(.*?)(?=") 并在没有全局设置的情况下逐行阅读内容,所以我只需获得每行的第一场比赛.在我的代码中,我只是将一个巨大的字符串放入代码中的 var 中,而不是实际使用 ajax/filereader 打开文件或设置表单来输入数据.我想我会将此标记为已解决,非常感谢!

Update: Alright so I think I'll go with (?<=")(.*?)(?=") and read things by line without the global setting on so I just get the first match on each line. In my code I was just plopping in a huge string into a var in the code instead of actually opening a file with ajax/filereader or having a form setup to input data. I think I'll mark this as solved, much appreciated!

推荐答案

你有两种选择来解决这个问题:

You have two choices to solve this problem:

使用捕获组

您可以匹配分隔符并使用捕获组来获取其中的文本.在这种情况下,您的两个正则表达式将起作用,但您需要使用访问捕获组 1 来获取结果 (demo).请参阅您如何访问匹配的组在 JavaScript 正则表达式中? 了解如何做到这一点.

You can match the delimiters and use capturing groups to get the text within. In this case your two regexes will work, but you need to use access capturing group 1 to get the results (demo). See How do you access the matched groups in a JavaScript regular expression? for how to do that.

使用零宽度断言

您可以使用零宽度断言仅匹配其中的文本,需要在它们周围使用分隔符而不实际匹配它们(演示):

You can use zero-width assertions to match only the text within, require delimiters around them without actually matching them (demo):

(?<=")(.*?)(?=")

但现在因为我没有使用引号,它会在每个引号之间找到实例,而不仅仅是在引号对之间:例如,a"b"c" 会找到 bc.

but now since I'm not consuming the quotes it'll find instances between each quote, not just between pairs of quotes: e.g., a"b"c" would find b and c.

至于获得第一场比赛,我认为这会在 JavaScript 中默认发生.在看到后续匹配之前,您必须要求重复匹配.所以如果你一次处理你的文件一行,你应该得到你想要的.

As for getting just the first match, I think that'll happen by default in JavaScript. You'd have to ask for repeated matching before you see the subsequent ones. So if you process your file one line at a time, you should get what you want.

这篇关于匹配简单分隔符,但不匹配分隔符本身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆