I need to pull a variable out of a URL or get an empty string if that variable is not present.
如果该变量不存在,我需要从URL中取出一个变量,或者获得一个空字符串。
Pseudo code:
伪代码:
String foo = "http://abcdefg.hij.klmnop.com/a/b/c.file?foo=123&zoo=panda";
String bar = "http://abcdefg.hij.klmnop.com/a/b/c.file";
when I run my regex I want to get 123 in the first case and empty string in the second.
当我运行regex时,我希望在第一种情况下得到123,在第二种情况下得到空字符串。
I'm trying this as my replace .*?foo=(.*?)&?.*
replacing this with $1
but that's not working when foo= isn't present.
我试着用这个来替换。*?foo=(.*?)&。*用$1替换它,但当foo=不存在时,它就不起作用。
I can't just do a match, it has to be a replace.
我不能只做一个匹配,它必须是一个替换。
4 个解决方案
#1
3
You can try this:
你可以试试这个:
[^?]+(?:\?foo=([^&]+).*)?
If there are parameters and the first parameter is named "foo", its value will be captured in group #1. If there are no parameters the regex will still succeed, but I can't predict what will happen when you access the capturing group. Some possibilities:
如果有参数,第一个参数被命名为“foo”,那么它的值将在组#1中被捕获。如果没有参数,regex仍然会成功,但是我无法预测当您访问捕获组时会发生什么。一些可能性:
- it will contain an empty string
- 它将包含一个空字符串
- it will contain a null reference, which will be automatically converted to
- an empty string
- 一个空字符串
- the word "null"
- “零”这个词
- 它将包含一个空引用,它将自动转换为空字符串“null”
- your app will throw an exception because group #1 didn't participate in the match.
- 你的应用会抛出一个异常,因为第一组没有参加比赛。
This regex matches the sample strings you provided, but it won't work if there's a parameter list that doesn't include "foo", or if "foo" is not the first parameter. Those options can be accommodated too, assuming the capturing group thing works.
这个regex匹配您提供的示例字符串,但是如果有一个不包含“foo”的参数列表,或者如果“foo”不是第一个参数,那么它就不能工作。这些选项也可以进行调整,假设捕获组的工作正常。
#2
1
I think you need to do a match, then a regex. That way you can extract the value if it is present, and replace it with "" if it is not. Something like this:
我认为你需要做一个匹配,然后是一个正则表达式。这样,如果值存在,就可以提取它,如果不存在,就用“”替换它。是这样的:
if(foo.match("\\?foo=([^&]+)")){
String bar = foo.replace("\\?foo=([^&]+)", $1);
}else{
String bar = "";
}
I haven't tested the regex, so I don't know if it will work.
我还没有测试regex,所以我不知道它是否会工作。
#3
0
In perl you could use this:
在perl中,您可以使用以下内容:
s/[^?*]*\??(foo=)?([\d]*).*/$2/
This will get everything up to the ? to start, and then isolate the foo, grab the numbers in a group and let the rest fall where they may.
这会把所有的东西都弄到?开始,然后隔离foo,在一个组中抓取数字,让其余的在它们可能的地方。
#4
0
There's an important rule when using regular expressions : don't try to put unnecessary processing into it. Sometimes things can't be done only by using one regular expression. Sometimes it is more advisable to use the host programming language.
在使用正则表达式时,有一个重要的规则:不要试图将不必要的处理放入其中。有时事情不能仅仅通过使用一个正则表达式来完成。有时,最好使用主机编程语言。
Marius' answer makes use of this rule : rather than finding a convoluted way of replacing-something-only-if-it-exists, it is better to use your programming language to check for the pattern's presence, and replace only if necessary.
马吕斯的回答利用了这条规则:与其寻找一种令人费解的方式来替换某个“如果”存在的东西,不如使用您的编程语言检查模式的存在,并在必要时进行替换。
#1
3
You can try this:
你可以试试这个:
[^?]+(?:\?foo=([^&]+).*)?
If there are parameters and the first parameter is named "foo", its value will be captured in group #1. If there are no parameters the regex will still succeed, but I can't predict what will happen when you access the capturing group. Some possibilities:
如果有参数,第一个参数被命名为“foo”,那么它的值将在组#1中被捕获。如果没有参数,regex仍然会成功,但是我无法预测当您访问捕获组时会发生什么。一些可能性:
- it will contain an empty string
- 它将包含一个空字符串
- it will contain a null reference, which will be automatically converted to
- an empty string
- 一个空字符串
- the word "null"
- “零”这个词
- 它将包含一个空引用,它将自动转换为空字符串“null”
- your app will throw an exception because group #1 didn't participate in the match.
- 你的应用会抛出一个异常,因为第一组没有参加比赛。
This regex matches the sample strings you provided, but it won't work if there's a parameter list that doesn't include "foo", or if "foo" is not the first parameter. Those options can be accommodated too, assuming the capturing group thing works.
这个regex匹配您提供的示例字符串,但是如果有一个不包含“foo”的参数列表,或者如果“foo”不是第一个参数,那么它就不能工作。这些选项也可以进行调整,假设捕获组的工作正常。
#2
1
I think you need to do a match, then a regex. That way you can extract the value if it is present, and replace it with "" if it is not. Something like this:
我认为你需要做一个匹配,然后是一个正则表达式。这样,如果值存在,就可以提取它,如果不存在,就用“”替换它。是这样的:
if(foo.match("\\?foo=([^&]+)")){
String bar = foo.replace("\\?foo=([^&]+)", $1);
}else{
String bar = "";
}
I haven't tested the regex, so I don't know if it will work.
我还没有测试regex,所以我不知道它是否会工作。
#3
0
In perl you could use this:
在perl中,您可以使用以下内容:
s/[^?*]*\??(foo=)?([\d]*).*/$2/
This will get everything up to the ? to start, and then isolate the foo, grab the numbers in a group and let the rest fall where they may.
这会把所有的东西都弄到?开始,然后隔离foo,在一个组中抓取数字,让其余的在它们可能的地方。
#4
0
There's an important rule when using regular expressions : don't try to put unnecessary processing into it. Sometimes things can't be done only by using one regular expression. Sometimes it is more advisable to use the host programming language.
在使用正则表达式时,有一个重要的规则:不要试图将不必要的处理放入其中。有时事情不能仅仅通过使用一个正则表达式来完成。有时,最好使用主机编程语言。
Marius' answer makes use of this rule : rather than finding a convoluted way of replacing-something-only-if-it-exists, it is better to use your programming language to check for the pattern's presence, and replace only if necessary.
马吕斯的回答利用了这条规则:与其寻找一种令人费解的方式来替换某个“如果”存在的东西,不如使用您的编程语言检查模式的存在,并在必要时进行替换。