简单的正则表达式问题,我只是无法弄清楚

时间:2023-01-27 14:56:02

I have quite a simple problem, I need to parse through this string:

我有一个非常简单的问题,我需要解析这个字符串:

"user_id" => "BXoVWYg-T_QS"

And grab only the BXoVWYg-T_QS

并且只抓住BXoVWYg-T_QS

I have used regex thus far to grab that much from a much larger string, but I can't figure out how to get it down to just the contents of the user_id string

到目前为止,我已经使用正则表达式来从一个更大的字符串中获取那么多,但是我无法弄清楚如何将其归结为user_id字符串的内容

This is the regex I have so far:

这是我到目前为止的正则表达式:

"user_id" => ".*"

Please help!

请帮忙!

Update I have a string of html that I am trying to parse through for an iPhone app in objective-c. If I can just get the regex syntax down, my problems will be solved

更新我有一串html,我试图解析Objective-c中的iPhone应用程序。如果我能够正确地获得正则表达式语法,我的问题将得到解决

3 个解决方案

#1


1  

"user_id" => "([^"]*)"

#2


1  

You need to indicate your capture using parens like this:

你需要使用这样的parens表示你的捕获:

"user_id" => "(.*)"

You can then locate that match. Are you using JavaScript, Perl, C#, ???

然后,您可以找到该匹配项。你使用的是JavaScript,Perl,C#,???

#3


1  

The problem is that .* is greedy -- so it grabs everything up to the last available " in your line / string (depending on the mode you are using).

问题是。*是贪婪的 - 所以它抓住你的行/字符串中的最后一个可用的东西(取决于你使用的模式)。

What you need is to either:

你需要的是:

A) Make the regex non-greedy. .*? will do that in some platforms.
B) Specifically exclude " from the list of characters you'd like to match: "user_id" => "([^"]*)" is what you'll want then.

A)使正则表达式不贪婪。 。*?会在某些平台上做到这一点。 B)明确排除“你想要匹配的字符列表:”user_id“=>”([^“] *)”就是你想要的。

#1


1  

"user_id" => "([^"]*)"

#2


1  

You need to indicate your capture using parens like this:

你需要使用这样的parens表示你的捕获:

"user_id" => "(.*)"

You can then locate that match. Are you using JavaScript, Perl, C#, ???

然后,您可以找到该匹配项。你使用的是JavaScript,Perl,C#,???

#3


1  

The problem is that .* is greedy -- so it grabs everything up to the last available " in your line / string (depending on the mode you are using).

问题是。*是贪婪的 - 所以它抓住你的行/字符串中的最后一个可用的东西(取决于你使用的模式)。

What you need is to either:

你需要的是:

A) Make the regex non-greedy. .*? will do that in some platforms.
B) Specifically exclude " from the list of characters you'd like to match: "user_id" => "([^"]*)" is what you'll want then.

A)使正则表达式不贪婪。 。*?会在某些平台上做到这一点。 B)明确排除“你想要匹配的字符列表:”user_id“=>”([^“] *)”就是你想要的。