Derick Marfo:
タイトルが示すように、どのように私は、Javaに新しいもの、Javaで、正確には、正規表現と対になっていない括弧や括弧を取り込むようにしてください。例えば、私は以下の文字列を持っていると仮定。
Programming is productive, (achieving a lot, and getting good results), it is often 1) demanding and 2) costly.
どのように私は、1)と2)を取得します。私が試してみました:
([^\(\)][\)])
)しかし、私は取得しています結果はSを含んで)以下のように、代わりの1)と2:
s), 1) and 2)
:私は、リンクチェックしたバランスの取れた括弧にマッチする正規表現を疑問が私の状況とは全く異なる、再帰的またはネストされた構造に言及しているように見える、しかし。私のような状況は、関連する左括弧やブラケットを持っていない任意の関連したテキストと一緒に、右括弧または右ブラケットを一致させることです。
エマ:
多分、
\b\d+\)
単に所望の出力を返すことがあります、私は推測します。
デモ1
もう一つの方法は、あなたが持っているかもしれないものの境界残って確認することです、この場合には、私は数字を見ているし、どのような他の文字は、私たちは前に閉じ中括弧にする必要があるだろう、と我々はに似て、いくつかの他の単純な式を設計することができます。
\b\d[^)]*\)
デモ2
テスト
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegularExpression{
public static void main(String[] args){
final String regex = "\\b\\d[^)]*\\)";
final String string = "Programming is productive, (achieving a lot, and getting good results), it is often 1) demanding and 2) costly.\n\n"
+ "Programming is productive, (achieving a lot, and getting good results), it is often 1a b) demanding and 2a a) costly.\n\n\n"
+ "Programming is productive, (achieving a lot, and getting good results), it is often 1b) demanding and 2b) costly.\n\n"
+ "It is not supposed to match ( s s 1) \n";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
出力
Full match: 1)
Full match: 2)
Full match: 1a b)
Full match: 2a a)
Full match: 1b)
Full match: 2b)
Full match: 1)
正規表現サーキット
jex.imは、正規表現を可視化します: