LoginSignup
2
1

More than 5 years have passed since last update.

お問い合わせメールから商品番号を正規表現で抜き出す

Posted at

これらの商品の購入を考えていますが、大量発注できますか?

商品番号:foo, bar, hoge

こんな感じの問い合わせメールが送信されるとして、
この文章から商品番号(ここでは foo と bar と hoge)を正規表現で抜き出したい。
サンプルを書いて、正規表現を試した。

FindProductNoSample.java
import java.util.regex.*;
public class FindProductNoSample {
    public static void main(String[] args) {
        String s = "商品番号:foo, bar, hoge";
        String p = "(?:^商品番号:|\\G, )(\\w+)";
        Pattern pt = Pattern.compile(p);
        Matcher mt = pt.matcher(s);
        while (mt.find()) {
            System.out.println(mt.group(1));
        }
    }
}

実行結果はこうなる。
foo
bar
hoge

参考URL
java - RegEx Advanced : Positive lookbehind - Stack Overflow
Pattern (Java Platform SE 7 )

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1