java 获取两个引号之间的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7933235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get a string in between two quote marks
提问by Austin
How would I get a value in between two the quotes after value=?
如何在 value= 之后的两个引号之间获得一个值?
So, value="hi my name is bob" />
would return: hi my name is bob
or value="Ouch! "that hurt" lol..." />
would return: Ouch! "that hurt" lol...
所以,value="hi my name is bob" />
会返回:hi my name is bob
或value="Ouch! "that hurt" lol..." />
会返回:Ouch! "that hurt" lol...
I know the value=" TEXT_HERE " />
will always occur and I want the string inside of it. and yes, there is always a space before the />
at the end. It is HTML code I am parsing, I have gotten everything except for this field to parse correctly.
我知道value=" TEXT_HERE " />
总是会发生,我想要它里面的字符串。是的,最后总是有一个空格/>
。这是我正在解析的 HTML 代码,除了这个字段之外,我已经得到了正确解析的所有内容。
EDITLet me clarify a little bit. I can't really use any side tools because I am using Webdriver to parse the page, after I get the source I throw the HTML into a string and then I try to parse the "value" tag out of all that data.
So the regex code has to be able to maneuver through all kinds of coding and get whatever the value field is. And I need every value field's data.
编辑让我澄清一点。我真的不能使用任何辅助工具,因为我使用 Webdriver 来解析页面,在获得源代码后,我将 HTML 放入一个字符串中,然后尝试从所有数据中解析“值”标签。
因此,正则表达式代码必须能够通过各种编码并获得任何值字段。我需要每个值字段的数据。
回答by Michael Fox
You could use String.indexOf()
to search for the first occurrence of "
. Save the first occurrence index, get the last occurrence index using String.lastIndexOf()
and call String.substring() to get the substring you want out.
您可以使用String.indexOf()
搜索第一次出现的"
。保存第一个出现索引,使用获取最后一个出现索引String.lastIndexOf()
并调用 String.substring() 以获取您想要的子字符串。
回答by André Ricardo
回答by Jiri Patera
I recommend using XPath
to do the job it was designed for. Here is an example that should get you on the track:
我建议使用它XPath
来完成它设计的工作。这是一个应该让你走上正轨的例子:
import java.io.ByteArrayInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
public class Test {
public static void main(String[] args) throws Exception {
String s = ""
+ "<?xml version=\"1.0\"?>"
+ "<root>"
+ " <a value=\"hello\" />"
+ " <b value=\'hello\' />"
+ " <c value=\"hello "bob"\" />"
+ "</root>";
ByteArrayInputStream bis = new ByteArrayInputStream(s.getBytes());
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document d = builder.parse(bis);
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
XPathExpression xpe = xpath.compile("//@value");
NodeList nl = (NodeList)xpe.evaluate(d, XPathConstants.NODESET);
for (int i = 0; i < nl.getLength(); i++) {
System.out.println(nl.item(i).getNodeValue());
}
}
}
The output is then:
然后输出是:
hello
hello
hello "bob"
回答by Zim
Here is some Java code and regex pattern that will work for you:
以下是一些适用于您的 Java 代码和正则表达式模式:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Pattern pattern = Pattern.compile("[\d\w\s'\"]+\z");
Matcher matcher = pattern.matcher("value=\"hi my name is bob\"");
while (matcher.find()) {
System.out.print("found:'"+matcher.group()+"'");
}
prints...
印刷...
found:'"hi my name is bob"'
找到:'“嗨,我的名字是鲍勃”'
You'll need to escape the quotes in your strings with \.
您需要使用 \ 对字符串中的引号进行转义。
回答by Mechkov
You can use regex to get the value between the quotes or you can work with the string that holds the whole statement/sentence/value.
您可以使用正则表达式来获取引号之间的值,也可以使用包含整个语句/句子/值的字符串。
For example you can use String.replaceAll method to replace all '"' (quotes) with '' (empty spaces).
例如,您可以使用 String.replaceAll 方法将所有 '"'(引号)替换为 ''(空格)。
回答by JRFerguson
In general:
一般来说:
echo 'value="hi my name is bob" />' | perl -nle 'm{value="\s*([^"]*)} and print '