我处在一个严格的Java环境中。
所以这个问题并不是真的像在这个问题上那么简单,我并不是在试图解决我所拥有的问题,而是更多地从理论上获得更好的知识。
我感兴趣的是用双引号或简单报价与src进行匹配,但如果是双引号,也必须以双引号结束,简单报价也是如此。
我知道我可以重复一遍,即:
String str = "src=\"hello/\" ... src='hello/' ..."
println str.replaceAll ("src=((\"[^\"]+\")|('[^']+'))", "src=$1")我想做的是:
println s.replaceAll ("src=([\"'][^\"']+[\"'])", "src=$1")但是,如果它以双引号开头,那么内容中应该允许简单引号,并且必须以双引号结束,而不是简单引用。
问题2:
是否有可能让它与找到的相同类型的引号replaceAll?可以说,在这个匹配中,用this2替换,用that2替换。在每次不生成新字符串的情况下,如何才能做到这一点?
为Alan More编辑,例如问题2:
println "one ... two".replaceAll( "(one)", "1" ).replaceAll("(two)", "2");更多地沿着这些路线(不对)
println "one ... two".replaceMyMatches( "(one)[^\\w]+(two)", "\$1{1}, \$2{2}" ) // prints string : one{1}, two{2} 我想要的是字符串: 1,2
对第一个问题的回答从黑熊猫和杰夫·沃克那里得到并改变了一点:
String str = "src=\"1.png\" ... src='2.jpeg' ... src=\"3.p'ng\" ... src='4.jpe\"g' ... src='' ... src=\"\" ..." ;
String regex = "src=(['\"])(.+?)\\1"; // closes with the quote that is in group 1
println str.replaceAll( regex, '''src=$1../new_path/$2$1''')吐出来:
src="../new_path/1.png" ... src='../new_path/2.jpeg' ... src="../new_path/3.p'ng" ... src='../new_path/4.jpe"g' ... src='' ... src="" ...如果一个人也想替换空的,只需在正则表达式中对一个恒星切换+(我不想那样)
注意,原来的引号也在里面。
回答问题2见黑熊猫
发布于 2012-01-19 14:54:54
我对第一项质询的答覆,本来是不正确的。这是最新版本。
若要回答问题1..See,此正则表达式是否对您有帮助:模式如下:
src=(['"])(.*?)\1下面的代码解释了每一段。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex {
public static void main(String[] args)
{
final String regex = "src=(['\"])" // the ' or the " is in group 1
+ "(.*?)" // match any character in a non-greedy fashion
+ "\\1"; // closes with the quote that is in group 1
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher("src=\"hello/\" ... src='goodbye/' ... "
+ "src='this has a \" in it'");
while (m.find())
{
System.out.println("\nfound!");
System.out.println("The quote was a " + m.group(1));
System.out.println("the text was = " + m.group(2));
}
}
}这给出了输出:
found!
The quote was a "
the text was = hello/
found!
The quote was a '
the text was = goodbye/
found!
The quote was a '
the text was = this has a " in it至于第二个问题,您必须使用更多的代码。您创建自己的StringBuffer并在执行过程中追加。我用了一张地图来代替:
public static void question2()
{
Pattern p = Pattern.compile("one|two");
Map<String, String> replacements = new HashMap<String, String>();
replacements.put("one", "1");
replacements.put("two", "2");
StringBuffer result = new StringBuffer();
String text = "one ... two";
Matcher m = p.matcher(text);
while (m.find())
{
m.appendReplacement(result, replacements.get(m.group()));
}
m.appendTail(result);
System.out.println(result.toString());
}这一产出如下:
1 ... 2发布于 2012-01-19 14:52:00
问题1的准则是:
src=(['"])hello\1 ( Java的双反斜杠)
它使用反向引用匹配第一个引号或双引号,然后与第一个引号匹配相同的字符。
所以对于更一般的情况,我喜欢:
^src=(['"])(.*?)\1$
那么替换可能是这样的:
String regex = "^src=(['\"])(.*?)\\1$";
String newthing = "src=$2";这就是你想要的吗?最基本的做法是剥去引号,同时强制它们匹配?
由于一个精明的评论,我现在明白了,你想让引号彼此脱身。像Perl这样的语言可以做到这一点,但它们不是通过regex解析的。这类事情属于需要实际解析的一类问题。(记不起实际的术语)
您必须检查组2并“断言”组1不存在,而不是替换。注意到我在正则表达式中添加了起始锚和结束锚。
所以,就像:
Pattern p = Pattern.compile("^src=(['\"])(.*?)\\1$");
Matcher m = p.matcher("src=\"what's up?\"");
if ( m.matches() ) {
if ( m.group(2).contains(m.group(1)) ) {
// fail, doesn't match
}
}
// success, follows all of the rules我很难理解你在第二个问题中寻找什么,即使是更新。如果我得到这个答案,我会编辑它。
发布于 2012-01-19 15:13:32
你可以试试这样的东西
String str = "src=\"hello/\" ... src='hello/' ...";
System.out.println(str.replaceAll("src=([\"'])(.*?)\\1", "src='$2'"));诀窍是在相同的正则表达式中使用\1来重用第一个匹配的模式
https://stackoverflow.com/questions/8927551
复制相似问题