5 Replies - 560 Views - Last Post: 04 April 2012 - 10:23 AM Rate Topic: -----

#1 metric  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 183
  • Joined: 22-May 10

Java regex

Posted 30 March 2012 - 12:42 PM

Hi guys,

I was wondering if someone could help with me design a regex snippet...

The input code string looks like:
%RANDOMSTUFF^LASTNAME,$FIRSTNAME^ADDRESS$CITY PROVINCE  POSTALCODE^?;RANDOMSTUFF


I would like to pull the lastname, firstname, address, city, province, and postalcode.

I'm trying to use regex groupings in java like so:
		CharSequence inputStr = "%RANDOMSTUFF^LASTNAME,$FIRSTNAME^ADDRESS$CITY PROVINCE  POSTALCODE^?;RANDOMSTUFF";
		String patternStr = "^([a-zA-Z ]+),\\$([a-zA-Z ]+)\\^([a-zA-Z0-9 ]+)\\$([a-zA-Z0-9 ]+)\\^";

		// Compile and use regular expression
		Pattern pattern = Pattern.compile(patternStr);
		Matcher matcher = pattern.matcher(inputStr);
		boolean matchFound = matcher.find();

		if (matchFound) {
		    // Get all groups for this match
		    for (int i=0; i<=matcher.groupCount(); i++) {
		        System.out.println(matcher.group(i));
		    }
		}



Thanks
Metric

This post has been edited by metric: 30 March 2012 - 01:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Java regex

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,637
  • Joined: 20-September 08

Re: Java regex

Posted 30 March 2012 - 01:53 PM

Quote

^LASTNAME,$FIRSTNAME


seems odd. Why use both a comma and '$' ?
Was This Post Helpful? 1
  • +
  • -

#3 metric  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 183
  • Joined: 22-May 10

Re: Java regex

Posted 30 March 2012 - 02:58 PM

View Postg00se, on 30 March 2012 - 12:53 PM, said:

Quote

^LASTNAME,$FIRSTNAME


seems odd. Why use both a comma and '$' ?


Good question...I don't know, this string is directly from a USB card reader :P
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,637
  • Joined: 20-September 08

Re: Java regex

Posted 31 March 2012 - 09:38 AM

OK. Can you post a text file with an actual example in it and i'll take a look?
Was This Post Helpful? 1
  • +
  • -

#5 metric  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 183
  • Joined: 22-May 10

Re: Java regex

Posted 31 March 2012 - 12:12 PM

View Postg00se, on 31 March 2012 - 08:38 AM, said:

OK. Can you post a text file with an actual example in it and i'll take a look?


Hi g00se,

I've attached it, though with minor modification (hiding my personal data). Thanks for taking a look!

Cheers
Metric

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#6 metric  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 183
  • Joined: 22-May 10

Re: Java regex

Posted 04 April 2012 - 10:23 AM

Okay guys,

I solved this, I figured I'd post the solution.

So I was so close. It was a basic syntax error.

I had:
^([a-zA-Z ]+),\$([a-zA-Z ]+)\^([a-zA-Z0-9 ]+)\$([a-zA-Z0-9 ]+)\^
notice the first ^, which has no escape character, in regex that means it was trying to match the beginning of the string.

I was actually trying to match characters between ^ and , so anyway here is the correct version:
\^([a-zA-Z ]+),\$([a-zA-Z ]+)\^([a-zA-Z0-9 ]+)\$([a-zA-Z0-9 ]+)\^
and as java string:
"\\^([a-zA-Z ]+),\\$([a-zA-Z ]+)\\^([a-zA-Z0-9 ]+)\\$([a-zA-Z0-9 ]+)\\^"

Thanks for the help guys!
Cheers
Metric
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1