2016-05-06 16 views
2

内の値のみを選択する:正規表現私はこの形式でいくつかの電子メールを持っているタグ

name of the person <[email protected]> 
name of another person <[email protected]> 

私はタグと一緒に、タグ内の要素だけを選択することになる正規表現式を持っていると思います..入力の上、この出力につながる:

<[email protected]> 
<[email protected]> 

私はあなたがたすべてのものと一致するように表現<[^>]*>を使用することができますJavaの

+0

なし '<' or '>'他の場所で、あなたの文字列ではありませんと仮定: '<.*>' – timolawl

+0

@timolawlを、これは最初の '<'と '最後>'の間のすべてを一致し、原因は正規表現が貪欲で、 '。*'はその間にすべて( '>')を消費します。 https://regex101.com/r/gV9uN5/1(改行には依存しないでください) – dognose

+0

この表現を試す 'String tagregex =" <(.*?)> ";' –

答えて

4

と一緒に正規表現行く仕事をしたいと思います1組の角度付きブレース内で:

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

// Define your regex 
Pattern regex = Pattern.compile("<[^>]*>"); 
// Get your matches 
Matcher m = regex.matcher("{your-input-here}"); 
// Iterate through your matches 
while(m.find()){ 
    // Output each match 
    System.out.println(m.group(0)); 
} 

see a working example of this hereです。

+0

ありがとう、その作品 –

1

だけでなく、既に回答されてなどの質問が、別のオプションは<.*?>を使用している、すなわち:

String text = "name of the person <[email protected]> name of another person <[email protected]>"; 
Pattern regex = Pattern.compile("<.*?>"); 
Matcher regexMatcher = regex.matcher(text); 
while (regexMatcher.find()) { 
    System.out.println(regexMatcher.group(0)); 
} 

デモ:

Java Demo

Regex Demo


正規表現の説明:

<.*?> 
    < matches the characters < literally 
    .*? matches any character (except newline) 
     Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy] 
    > matches the characters > literally 
+0

あなたの助けてくれてありがとう、リオンは最初に答えたので、とにかく、デモと正規表現の説明との良い仕事、ありがとう –

関連する問題