[英]Find a subtring in a string using a regular expression - JAVA
Suppose i have a string " kk abcjkmkc jjkocc abc jjj 'abckkkkkkkkkkkkkkkk ' " 假设我有一个字符串“ kk abcjkmkc jjkocc abc jjj'abckkkkkkkkkkkkkkkkkk'”
I want to replace the substring abc in the string which are only outside the single quote , but it is not working. 我想替换仅在单引号之外的字符串中的子字符串abc,但不起作用。
Here is my code ` 这是我的代码
String str = " kk a.b.cjkmkc jjkocc a.b.c. jjj 'a.b.ckkkkkkkkkkkkkkkk ' ";
Pattern p = Pattern.compile("a\\.b\\.c");
Matcher m = p.matcher(str);
int x = m.find()
`
To search for a substring outside quotes, you can do something like this: 要在引号外搜索子字符串,可以执行以下操作:
Pattern pat = Pattern.compile("^(?:[^']|'[^']*')*?a\\.b\\.c");
The first part will skip over: 第一部分将跳过:
every character that isn't a quote mark ( [^']
), or 不是引号(
[^']
)的每个字符,或
every sequence of non-quote-mark characters enclosed in quotes ( '[^']*'
). 用引号(
'[^']*'
)括起来的每个非引号字符序列。
Once those are skipped, then if it sees the pattern you want, it will know that it isn't inside quote marks. 一旦跳过了这些内容,则如果看到所需的模式,它将知道它不在引号内。
This will handle a simple case. 这将处理一个简单的情况。 If things start getting more complicated, eg you want to allow
\\'
to quote a quote mark in your input string the way C or Java does in a string literal, the regex starts getting more complicated, and you can quickly reach a point whether either your regex is unreadable or regexes aren't suitable solutions. 如果事情开始变得更加复杂,例如,您想允许
\\'
在输入字符串中用引号引起来,就像C或Java在字符串文字中一样,则正则表达式开始变得更加复杂,您可以快速地得出一个结论:您的正则表达式不可读或正则表达式不合适。
EDIT: fixed to put "reluctant" qualifier after second
*
, so that the first abc
will be found. 编辑:修复了在第二个
*
之后放置“不愿意”的限定词,以便找到第一个abc
。
EDIT 2: If you want to replace the substring you find, it gets trickier.
编辑2:如果要替换找到的子字符串,它将变得更加棘手。 The above pattern matches the entire beginning of the string up through
abc
, and I couldn't get a look-behind to work so that the match would be only the abc
part. 上面的模式通过
abc
匹配字符串的整个开头,而我无法进行后视工作,因此匹配项仅是abc
部分。 I think you'll need to put the beginning of the string in a group, and then use $1
in the replacement string to copy the beginning: 我认为您需要将字符串的开头放入组中,然后在替换字符串中使用
$1
复制开头:
Pattern pat = Pattern.compile("^((?:[^']|'[^']*')*?)a\\.b\\.c");
Matcher m = pat.matcher(source);
if (m.find()) {
result = m.replaceFirst("$1replacement");
}
I'm not sure replaceAll
works with this, so if you want to replace all of them, you may need to loop. 我不确定
replaceAll
可以与此一起使用,因此,如果要替换所有它们,则可能需要循环。
使用以下模式: a\\.b\\.c(?=(([^']*'){2})*[^']*$)
演示
I wouldn't mess with REGEX.
我不会理会REGEX。
public static void main(String[] args) {
String str = " kk a.b.cjkmkc jjkocc a.b.c. jjj 'a.b.ckkkkkkkkkkkkkkkk ' ";
String[] s = str.split("'");
str = s[0].replaceAll("[abc]", "") + "'"+ s[1]+"'"
+ s[2].replaceAll("[abc]", "");
System.out.println(str);
}
OP: OP:
kk ..jkmk jjko ... jjj 'a.b.ckkkkkkkkkkkkkkkk '
Inefficient.. but works 低效..但是有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.