简体   繁体   English

正则表达式以查找##之间的字符

[英]Regular expression to find characters between ##

I have text as below 我有以下文字

Here is some text #variable name# that goes very long with #another variable name# and 
goes longer #another another variable# and some more.

I would like to write a regular expression that splits this text into groups like this 我想写一个正则表达式,将文本分成这样的组

Group 1: Here is some text 
Group 2: #variable name#
Group 3: that goes very long with 
Group 4: #another variable name#
Group 5: and goes longer 
Group 6: #another another variable#
Group 7: and some more

My attempt is poor. 我的尝试很糟糕。 I am not able to get my head around this thing 我无法解决这个问题

(.*?)*(#.*#)*(.*?)*

Also, this needs to work in Java.. like the below 另外,这需要在Java中工作。

    import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.*;

public class Test {

    public static void main(String[] args) {
        Pattern pattern = compile("(([^#]+)|(#[^#]+#)) ");
        String string="Here is some text #variable name# that goes very long with #another variable name# and " +
                "goes longer #another another variable# and some more.";
        Matcher matcher = pattern.matcher(string);
        while(matcher.find()){
            System.out.println(matcher.group());
        }
    }
}
([^#]+)|(#[^#]+#)

Try this.Grab the captures.See demo.use g flag or global match. 试试看。获取捕获。请参阅demo.use g标志或全局匹配。

http://regex101.com/r/pQ9bV3/1 http://regex101.com/r/pQ9bV3/1

You could try the below regex, 您可以尝试以下正则表达式,

(#[^#]*#)|\s*(.*?)(?=\s*(?:#|$))

DEMO 演示

(.*?[^\#])|(#.*#)

试试这个正则表达式

This would seem to be what you are looking for: 这似乎是您要查找的内容:

([^#]+|#[^#]+#)

正则表达式可视化

Or if you want the white-space trimmed: 或者,如果您要修剪空白:

\s*([^#]+?(?=\s*#|$)|#[^#]+#)

正则表达式可视化

one more pattern to have trimmed results 另一种模式可以修剪结果

(#[^#]+#|[^#]+)(?:\s|$)

Demo 演示版

(               Capturing Group \1
  #             "#"
  [^#]          Character not in  [^#]
  +             (one or more)(greedy)
  #             "#"
  |             OR
  [^#]          Character not in  [^#]
  +             (one or more)(greedy)
)               End of Capturing Group \1
(?:             Non Capturing Group
  \s            <whitespace character>
  |             OR
  $             End of string/line
)               End of Non Capturing Group

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

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