Solved

# Regular Expression for a particular input type !

Posted on 2007-08-10
181 Views
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
Question by:royalcyber

LVL 86

Expert Comment

As i mentioned earlier, you'd probably be better off ignoring the penultimate argument
0

LVL 40

Expert Comment

Can you try ...

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

0

LVL 40

Accepted Solution

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

Author Comment

How do i use the above regex in java to split it into the different array elements?
0

LVL 86

Expert Comment

php?
0

LVL 86

Expert Comment

Why do you need a regex?
0

LVL 40

Expert Comment

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

LVL 92

Expert Comment

> 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

Author Comment

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

Author Comment

CEHJ.. Not necessary regex.. just wanna parse.. any other way around?
0

LVL 92

Expert Comment

Thats what I posted :)
0

LVL 92

Assisted Solution

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

Author Comment

But thats not wrking for the regex RQuadling posted and the str - 1 book at 10 .. :(
0

## Featured Post

### Suggested Solutions

hash value 2 29
matchUp  challenge 9 51
countPairs challenge 7 39
pairs challenge 5 33
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.