Java Pattern Regex Matcher. Look for string inside text

the code bellow help you to auto detect if the input text contains pattern like “Purchase Order number, customer number…”

	// pattern can be something like "[A-Z]{2}-\\w{3}-\\w{5}" so this;
	public static List<String> getResultList(String srcText, String regexStr) {
		List<String> result = new ArrayList<String>();
		try {
			final Pattern myPattern = Pattern.compile(regexStr);
			Matcher m = myPattern.matcher(srcText);
			while (m.find()) {
		} catch (Exception e) {
			return null;

		if (result.isEmpty()) {
			return null;
		return result;

	public static void main(String[] args) {
		String text1 = "Purchase Order N° [CU-USA-B0052]";
		String text2 = " Please Confirm the Purchase Order N° QT-YAZ-A1983 and Cancel Purchase Order Number [QT-USA-A0001]";
		List<String> poNumbers = getResultList(text2, "QT-\\w{3}-\\w{5}");
		if (poNumbers != null) {
			for (String po : poNumbers) {
				System.out.println("Purchase Order Num. Found = " + po);

A java Developer interested in Big Data,NoSQL, Functionnal and Dynamic programming

Tagged with: , ,
Posted in Java_corner

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: