在正则表达式中转义

在正则表达式中转义

本文介绍了Java,在正则表达式中转义(使用)引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Java中使用以下正则表达式,它应该匹配任何 lang =2-char-lang-name

  String lang =lang = \+ L.detectLang(inputText)+\; 
shovel.replaceFirst(lang = \[..] \,lang);

我知道一个斜杠将被正则表达式解释为斜杠而不是转义字符我的代码不起作用),但是如果我逃避斜杠,那么将不再被转义,我会得到一个语法错误。



换句话说,如何在正则表达式中包含一个lang = \\[..] \\将无法正常工作。我也尝试过三次斜杠,也没有任何匹配。



我也知道您不使用正则表达式解析XML的一般规则/ HTML。 (和 shovel 是一个XML)但是,我所做的是,寻找一个 lang 属性XML的前30个字符,我想替换它。在这种情况下使用正则表达式真的是个坏主意吗?我不认为使用DOM会更好/更有效率。

解决方案

三个斜杠是正确的( \\ + \成为 \ + = \)(更新:实际上,事实证明,一个单一的斜杠也可以工作,似乎。)问题是你使用 [..] ; [] 符号表示这里的任何字符(所以 [..] 只是意味着任何字符)。



删除 [] ,你应该得到你想要的:

  String ab =foo = \bar\lang = \AB \; 
String regex =lang = \\\.. \\\ \\\;
String cd = ab.replaceFirst(regex,lang = \CD \);
System.out.println(cd);

输出:

  foo =barlang =CD


I'm trying to use the following regex in Java, that's supposed to match any lang="2-char-lang-name":

String lang = "lang=\"" + L.detectLang(inputText) +"\"";
shovel.replaceFirst("lang=\"[..]\"", lang);

I know that a single slash would be interpreted by regex as a slash and not an escape character (so my code doesn't work), but if I escape the slash, the " won't be escaped any more and I'd get a syntax error.

In other words, how can I include a " in the regex? "lang=\\"[..]\\"" won't work. I've also tried three slashes and that didn't have any matches either.

I am also aware of the general rule that you don't use regex to parse XML/HTML. (and shovel is an XML) However, all I'm doing is, looking for a lang attribute that is within the first 30 characters of the XML, and I want to replace it. Is it really a bad idea to use regex in this case? I don't think using DOM would be any better/more efficient.

解决方案

Three slashes would be correct (\\ + \" becomes \ + " = \"). (Update: Actually, it turns out that isn't even necessary. A single slash also works, it seems.) The problem is your use of [..]; the [] symbols mean "any of the characters in here" (so [..] just means "any character").

Drop the [] and you should be getting what you want:

String ab = "foo=\"bar\" lang=\"AB\"";
String regex = "lang=\\\"..\\\"";
String cd = ab.replaceFirst(regex, "lang=\"CD\"");
System.out.println(cd);

Output:

foo="bar" lang="CD"

这篇关于Java,在正则表达式中转义(使用)引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 22:05