English 中文(简体)
正则表达式:如何忽略行的其余部分
原标题:regular expression: how to ignore rest of the line
  • 时间:2010-10-06 11:20:53
  •  标签:
  • regex

我有这样的输入(JSON格式)

{“location”:[{“id”:“1BCDEFGHIJKLM”,“somename”:“abcd”,“fewlame”:“xyzland”,“sid”:“”,“sname”:“,”regioname“:”Zee Whole“,”type“:”some“,”siteCode“:”“,”someCode“:“ROTXY”,”fewlCode“:HMOPRE“,”somename“:”abcd Junction“,”fewname“:”United States“,”sid“:”“,”sname“:”“USRTJ”,“fewlCode”:“US”,“pCode”:“USNWK”,“someid”:“7823YZHMOPRE”,“Fewlid”:“:”NL“,”pCode“:”“,”someid“:”799XYZHMOPRE“,”fewlid“:“OIUOWER348534”}]}

现在,我想使用正则表达式获取第一个“id”值,即1BCDEFGHIJKLM。我用

[^({“location”:[?{“id”:“)].{0,12}但这是不完整的。有人能帮助我如何忽略值1BCDEFGHIJKLM之后的其余部分吗

问题回答

Regex isn t the way to do this. Whatever platform you are using, it must have a JSON parser. That will be your best error-free solution.


假设您必须使用regex,则可以使用“id”:“(.*?)”获取所有id,并进行第一个匹配。

我发现了以下文章,这可能会对您有所帮助。

虽然很混乱,但您的正则表达式是如何不完整的?

它可以缩短为(“id”:“([^”]+)”,这更具可读性,并且不将id限制在12个字符以内。如果这样做有益的话。

如果你的问题是得到多个结果,大多数语言都有一个“g”全局开关。

在javascript中,以下内容将返回“1BCDEFGHIJKLM”:

var firstID = str.match(/"id":"([^"]+)"/)[1]

As match()返回一个数组,其中[0]是返回的整个字符串,[1]是第一个括号。

不必使用正则表达式。用你最喜欢的语言,用逗号分隔。然后浏览每个项目,检查“id”并在冒号(:)上拆分。获取最后一个元素。例如Python

>>> s
 {"location":[{"id":"1BCDEFGHIJKLM","somename":"abcd","fewname":"xyzland","sid":"","sname":"","regionname":"Zee-Whole","type":"some","siteCode":"","someCode":"ROTXY","fewCode":"NL","pCode":"ROTXY","someid":"1BCDEFGHIJKLM","fewid":"GIC8"},{"id":"7823XYZHMOPRE","somename":"abcd Junction","fewname":"United States","sid":"","sname":"","regionname":"New York","type":"some","siteCode":"","someCode":"USRTJ","fewCode":"US","pCode":"USNWK","someid":"7823XYZHMOPRE","fewid":"7823XYZLMOPRE"},{"id":"799XYZHMOPRE","somename":"abcd-Maasvlakte","fewname":"xyzland","sid":"","sname":"","regionname":"Zee-Whole","type":"some","siteCode":"","someCode":"XYROT","fewCode":"NL","pCode":"","someid":"799XYZHMOPRE","fewid":"OIUOWER348534"}]} 

>>> for i in s.split(","):
...     if  "id"  in i:
...         print i.split(":")[-1]
...         break
...
"1BCDEFGHIJKLM"

当然,理想情况下,您应该使用专用的JSON解析器。





相关问题
Uncommon regular expressions [closed]

Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.

regex to trap img tag, both versions

I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />

C++, Boost regex, replace value function of matched value?

Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

Is it possible to negate a regular expression search?

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...

热门标签