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()) {
				result.add(m.group());
			}
		} catch (Exception e) {
			// DO SOMETHING HERE
			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);
			}
		}
	}
...
About these ads
About

building mycareer path to become a CIO

Tagged with: , ,
Posted in Uncategorized

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: