[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

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



      
0
royalcyber
Asked:
royalcyber
  • 4
  • 3
  • 3
  • +1
2 Solutions
 
CEHJCommented:
As i mentioned earlier, you'd probably be better off ignoring the penultimate argument
0
 
Richard QuadlingSenior Software DeverloperCommented:
Can you try ...

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

0
 
Richard QuadlingSenior Software DeverloperCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 DeverloperCommented:
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
 
objectsCommented:
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now