简体   繁体   中英

How to match anything between two characters?

How can I write a regex that matches anything between two specific characters?

like:

ignore me [take:me] ignore me ?

How can I match inclusive [take:me] ?

The word take:me is dynamic, so I'd also would like to match [123as d:.-,§""§%]

You can use this regex:

"\\\\[(.*?)\\\\]"

This link should help you to understand why it works.

Pattern pattern = Pattern.compile("\\[(.*?)\\]");
Matcher matcher = pattern.matcher("ignore me [take:me] ignore me");
if (matcher.find()) {
    System.out.println(matcher.group(1));
}

This will print take:me .


If you want to match &([take:me]) you should use this:

&\\\\(\\\\[(.*?)\\\\]\\\\)

Not that you should escape chars with special meaning in regex. (like ( and ) ).

Escaping them is done by adding a backslash, but because backslash in Java is written as \\\\ then you add \\\\ before any char that have a special meaning. So by doing \\\\( you're telling Java: " Take ( as the regular char and not the special char ".

The java.util.regex.Matcher class is used to search through a text for multiple occurrences of a regular expression. You can also use a Matcher to search for the same regular expression in different texts.

The Matcher class has a lot of useful methods. For a full list, see the official JavaDoc for the Matcher class. I will cover the core methods here. Here is a list of the methods covered:

Creating a Matcher

Creating a Matcher is done via the matcher() method in the Pattern class. Here is an example:

String text    =
    "This is the text to be searched " +
    "for occurrences of the http:// pattern.";
String patternString = ".*http://.*";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(text);

matches()

The matches() method in the Matcher class matches the regular expression against the whole text passed to the Pattern.matcher() method, when the Matcher was created. Here is an example:

boolean matches = matcher.matches();

If the regular expression matches the whole text, then the matches() method returns true. If not, the matches() method returns false.

You cannot use the matches() method to search for multiple occurrences of a regular expression in a text. For that, you need to use the find(), start() and end() methods.

lookingAt()

The lookingAt() method works like the matches() method with one major difference. The lookingAt() method only matches the regular expression against the beginning of the text, whereas matches() matches the regular expression against the whole text. In other words, if the regular expression matches the beginning of a text but not the whole text, lookingAt() will return true, whereas matches() will return false.

Here is an example:

String text    =
    "This is the text to be searched " +
    "for occurrences of the http:// pattern.";
String patternString = "This is the";
Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(text);
System.out.println("lookingAt = " + matcher.lookingAt());
System.out.println("matches   = " + matcher.matches());

find() + start() + end()

The find() method searches for occurrences of the regular expressions in the text passed to the Pattern.matcher(text) method, when the Matcher was created. If multiple matches can be found in the text, the find() method will find the first, and then for each subsequent call to find() it will move to the next match.

The methods start() and end() will give the indexes into the text where the found match starts and ends.

Here is an example:

String text    =
    "This is the text which is to be searched " +
    "for occurrences of the word 'is'.";
String patternString = "is";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(text);
int count = 0;
while(matcher.find()) {
  count++;
 System.out.println("found: " + count + " : "
 + matcher.start() + " - " + matcher.end());
}

This example will find the pattern "is" four times in the searched string. The output printed will be this:

found: 1 : 2 - 4

found: 2 : 5 - 7

found: 3 : 23 - 25

found: 4 : 70 - 72

You can also refer these tutorials..

Tutorial 1

You can also use lookaround assertions . This way the brackets are not included in the match itself.

(?<=\\[).*?(?=\\])

(?<=\\\\[) is a positive lookbehind assertion. It is true, when the char "[" is before the match

(?=\\\\]) is a positive lookahead assertion. It is true, when the char "[" is after the match

.*? is matching any character zero or more times, but as less as possible, because of the modifier ? . It changes the matching behaviour of quantifiers from "greedy" to "lazy".

尝试(?<=c)(.+)(?=c) ,其中c是您使用的字符

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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