Regular Expression for a particular input type !

Hi experts,
      I am new to regular expressions. Can somebody help me with a regular expression for the following problem. If my input format is of the following:
      
      1 book at 10
      1 Calendar at 20
      1 set of stickers at 5

The format of the string is <quantity> <item> at <price>

So, when i do a

      String[] s = str.split(<my regular exp>)

{str is the above input line by line,i.e '1 book at 10' first,'1 Calendar at 20'next and so on.. }

should return me an array which contains(for first line of input)
s[0] = 1
s[1] = book
s[2] = 10

array for 2nd line of input shud be
s[0] = 1
s[1] = Calendar
s[2] = 20

array for 3rd line of input shud be
s[0] = 1
s[1] = set of stickers
s[2] = 5



      
royalcyberAsked:
Who is Participating?
 
Richard QuadlingConnect With a Mentor Senior Software DeveloperCommented:
Ah. More whitespace and buggy correction.

<?php
$s_Text = <<<END_TEXT
      1 book at 10
      2 Calendar at 20
      3 set of stickers at 5

END_TEXT;

preg_match_all('/^\s*?(\d+) (.*?) at (\d+)[\r\n]*$/im', $s_Text, $a_Matches, PREG_SET_ORDER);

print_r($a_Matches);
?>

outputs ...

Array
(
    [0] => Array
        (
            [0] =>       1 book at 10
            [1] => 1
            [2] => book
            [3] => 10
        )

    [1] => Array
        (
            [0] =>       2 Calendar at 20
            [1] => 2
            [2] => Calendar
            [3] => 20
        )

    [2] => Array
        (
            [0] =>       3 set of stickers at 5

            [1] => 3
            [2] => set of stickers
            [3] => 5
        )

)

I don't know what the Java syntax is, but the regex is OK.

0
 
CEHJCommented:
As i mentioned earlier, you'd probably be better off ignoring the penultimate argument
0
 
Richard QuadlingSenior Software DeveloperCommented:
Can you try ...

/^(\d+) (.*) at (/d+)$/i

0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
royalcyberAuthor Commented:
How do i use the above regex in java to split it into the different array elements?
0
 
CEHJCommented:
php?
0
 
CEHJCommented:
Why do you need a regex?
0
 
Richard QuadlingSenior Software DeveloperCommented:
I don't know Java, but I can proove the regex using PHP.

I got notified of this question because of the regex tag against it.

The regex is ...

/^\s*?(\d+) (.*?) at (\d+)[\r\n]*$/im

I don't know how to tell Java to store the results in set order rather than pattern order (which is the default I believe).
0
 
objectsCommented:
> How do i use the above regex in java to split it into the different array elements?

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
boolean match = matcher.find();
if (match) {
   for (int i=0; i<3; i++) {
      s[i] = matcher.group(i+1);
   }
}

http://www.exampledepot.com/egs/java.util.regex/Group.html
0
 
royalcyberAuthor Commented:
RQuadling,thanx a lot for u r code.. Objects, can u please give me a wrking function to parse the above input, something like RQuadling's php function..
0
 
royalcyberAuthor Commented:
CEHJ.. Not necessary regex.. just wanna parse.. any other way around?
0
 
objectsCommented:
Thats what I posted :)
0
 
objectsConnect With a Mentor Commented:
something like this:

public String[] parse(String str) {
String[] s = new String[3];
Pattern pattern = Pattern.compile("(\d+) (.*?) at (\d+)"); // or use the regexp RQuadling posted
Matcher matcher = pattern.matcher(str);
boolean match = matcher.find();
if (match) {
   for (int i=0; i<3; i++) {
      s[i] = matcher.group(i+1);
   }
}
return s;
}
0
 
royalcyberAuthor Commented:
But thats not wrking for the regex RQuadling posted and the str - 1 book at 10 .. :(
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.