简体   繁体   English

匹配任何字符串正则表达式

[英]Match any string regex

I am attempting to match any string of charicters using regular expressions in Java but have encounted a problem. 我正在尝试使用Java中的正则表达式匹配任何字符字符串,但是遇到了问题。 I am trying to replace where a string contains %key% with the corrasponding value from a hashmap. 我试图用哈希图中对应的值替换包含%key%的字符串。

My currant regex pattern is %([.]+)% which seems to not work but I'm not sure why. 我的黑醋栗正则表达式模式是%([.]+)% ,这似乎不起作用,但我不确定为什么。 I have tried %([az AZ 0-9])% which seems to work fine but I would like to allow all charichters but new line and "." 我已经尝试了%([az AZ 0-9])% ,它似乎可以正常工作,但我想允许除新行和“。”之外的所有charichters。 does not seem to allow anything but fullstops(eg %...% will worb but %test% won't. 似乎除了句号之外什么都不允许(例如, %...%会烦扰,而%test%不会。

I'm honestly not sure what I have done wrong, I assume I am using the fullstop in the wrong place but can't seem to find how to use it correctly. 老实说,我不确定自己做错了什么,我想我在错误的地方使用句号,但似乎找不到正确的用法。

Sorry for the horrible explaination, I can't think of how to put it better. 很抱歉为您提供可怕的解释,我无法考虑如何更好地进行说明。

Thanks. 谢谢。

I would like to allow all charichters but new line and "."

You can use this negation based regex: 您可以使用以下基于否定的正则表达式:

%([^.%\\n\\r]*)%

[^.%\\\\n\\\\r] means match anything but DOT OR % OR new line characters [^.%\\\\n\\\\r]表示匹配DOT或%或换行符以外的任何内容

Use (?<=%)(.+?)(?=%) . 使用(?<=%)(.+?)(?=%)

EXPLANATION 说明

说明

Sample code: 样例代码:

HashMap<String , String> map=new HashMap<>();
map.put("1", "one");
map.put("2", "two");
map.put("3", "three");
String line="%123%";

Pattern pattern=Pattern.compile("(?<=%)(.+?)(?=%)");
Matcher matcher=pattern.matcher(line);
if(matcher.find()){
      String match=matcher.group();
      Iterator<String> it=map.keySet().iterator();
       while(it.hasNext()){
           String key=it.next();
           if(match.contains(key)){
                line=line.replace(key, map.get(key).toString());
           }

        }
            System.out.println(line);
 }

OUTPUT 输出值

%onetwothree%

Try: 尝试:

System.out.println("%sdfsdf%".matches("^%.+%$"));

I think in some situations Regex only is not enough - you should do some logic also. 我认为在某些情况下,仅Regex还不够-您还应该做一些逻辑。 You can try with StringBuilder . 您可以尝试使用StringBuilder

You want to use something like: 您想使用类似:

%([^.]+?)%

The ? ? means non-greedy. 表示非贪婪。 The [^.] means not full stop. [^.]表示不完全停止。

Given the input: 给定输入:

%inside1%something%inside2%

It will match %inside1% and %inside2% 它将匹配%inside1%%inside2%

The problem with [.] is that it is literally a full stop. [.]的问题在于它实际上是一个句号。

Edit If you are matching line by line, then mine will be fine. 编辑如果您逐行匹配,那么我的就可以了。 If you are matching across multiple lines then you should use anubhava answer which will explicitly exclude linebreaks from the match, so multiline queries that match % across lines will be excluded from the results. 如果您要在多行中进行匹配,则应使用anubhava答案,该答案将从匹配项中明确排除换行符,因此, 跨行匹配%的多行查询将从结果中排除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM