java 正则表达式忽略前导字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5716584/
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
Regex to ignore leading characters
提问by wobblycogs
I'm sure there's a simple solution to this but it's got me stuck so hopefully someone can give me a hand. I've got multiple lines of input that look like this:
我确信有一个简单的解决方案,但它让我陷入困境,所以希望有人能帮我一把。我有多行输入,如下所示:
FooABC
Foo XYZ123
FooFgh
I need a regex (in Java) that will give me:
我需要一个正则表达式(在 Java 中),它会给我:
ABC
XYZ123
Fgh
The best I've come up with so far is:
到目前为止我想出的最好的是:
[^Foo\s?].*$
Which I thought was working until I noticed that it was removing the capital F from Fgh. Is there any way I can make it exactly ignore the first three characters and one or more whitespace characters then just capture everything else?
我认为这是有效的,直到我注意到它正在从 Fgh 中删除大写 F。有什么办法可以让它完全忽略前三个字符和一个或多个空白字符,然后只捕获其他所有字符?
EDIT / CLARIFICATION
编辑/澄清
I should have said that I can't use substring / replace or any of the things I would normally do in this situation because this regex will be fed into deployed code that can't currently be updated. What the code is doing is this:
我应该说我不能使用子字符串/替换或在这种情况下我通常会做的任何事情,因为这个正则表达式将被输入到当前无法更新的部署代码中。代码在做什么是这样的:
Pattern pattern = Pattern.compile( regex );
Matcher matcher = pattern.matcher( data );
matcher.find();
String extracted = matcher.group();
and it's then using the extracted string as a key for a look up. So it has to extract the key in one pass.
然后它使用提取的字符串作为查找的键。所以它必须一次性提取密钥。
回答by kennytm
Replace
代替
^Foo\s*
with an empty string. Use .{3}
if you need 3 arbitrary characters instead of Foo
.
带有空字符串。使用.{3}
,如果你需要3个任意字符来代替Foo
。
If you need to use match instead of replace, you need to use a capture group:
如果需要使用匹配而不是替换,则需要使用捕获组:
^Foo\s*(.*)$
The [^Foo\s?]
is a character class ([...]
) to match any characters except (^
)an F
, a o
, a whitespace (\s
) or a question mark ?
. That's why the F
in Fgh
is also matched. If you want a grouping bracket, use (?:...)
.
的[^Foo\s?]
是一个字符类([...]
)来匹配任何字符除了(^
)的F
,一o
,一个空格(\s
)或问号?
。这就是F
inFgh
也匹配的原因。如果需要分组括号,请使用(?:...)
.
回答by Frank Schmitt
^.{3}\s+.*$
should do the trick (it matches 3 arbitrary characters at the beginning of the line, one or more whitespaces and then anything up until the end of the line).
应该可以解决问题(它匹配行首的 3 个任意字符,一个或多个空格,然后匹配行尾的任何字符)。
回答by Peter Lawrey
What about
关于什么
String text = text.substring(3).trim();
or KennyTM's solution
或 KennyTM 的解决方案
String text = text.replaceFirst("^Foo\s*", "");