regx quetsion

I am having problems coming up with a regx that will match the section of the string I am wanting. Can/will someone plesae help me? Here is what I am trying and it is not working.

publicstaticvoid main(String[] args){

String response ="ISA~00~ ~00~ ~ZZ~REMOVED~ZZ~REMOVED~200201~1137~<~00405~000000001~0~T~!|GS~PO~REMOVED~ORDRLS90~20060201~113744~000000001~X~004050|ST~850~0001|BEG~00~SS~12345ABC~~20060201|REF~11~7132901427~ATN|REF~V0~09.00|DTM~097~20060201~1137AM|SI~TI~TP~TX|SI~TI~RE~E|SI~TI~AA~W|SI~TI~TY~81G-|PID~S~~TI~AO~~~SO-RSQ~Y|N1~BY~~25~3176|SE~12~0001|GE~1~000000001|IEA~1~000000001|";

/**

* This is how I want to get the substring

* Begin with a '|GS' then at least one '~' then any number or chars followed by only one '|'

* therefore the subsring I hope to get is:

* |GS~PO~LOCALTELSWORDT ~ORDRLS90~20060201~113744~000000001~X~004050|

*/

String GSregex ="|(GS)(~+?).*(\\|{1})";

List list = getMatchingValues(GSregex, receivedEdiMessage);

//print out the list

for (Iterator iter = list.iterator(); iter.hasNext();){

String element = (String) iter.next();

System.out.println(GSregex);

System.out.println("\t"+element);

}

}

//gets the value of the regex out of a search string

private List getMatchingValues(String regex, String searchString){

Pattern pattern = Pattern.compile(regex);

Matcher matcher = pattern.matcher(searchString);

List list =new LinkedList();

while(matcher.find()){

list.add(matcher.group());

}

return list;

}

The problem is that when I run the above main I get

(\|GS)(~+?).*(\|{1})

|GS~PO~REMOVE~ORDRLS90~20060201~113744~000000001~X~004050|ST~850~0001|BEG~00~SS~12345ABC~~20060201|REF~11~7132901427~ATN|REF~V0~09.00|DTM~097~20060201~1137AM|SI~TI~TP~TX|SI~TI~RE~E|SI~TI~AA~W|SI~TI~TY~81G-|PID~S~~TI~AO~~~SO-RSQ~Y|N1~BY~~25~3176|SE~12~0001|GE~1~000000001|IEA~1~000000001|

and that's not what I want.

[2754 byte] By [doopsterusa] at [2007-10-2 13:42:48]
# 1
What is it that you want?
CeciNEstPasUnProgrammeura at 2007-7-13 11:37:39 > top of Java-index,Java Essentials,Java Programming...
# 2
> What is it that you want?Oh. Found it in the comment.
CeciNEstPasUnProgrammeura at 2007-7-13 11:37:39 > top of Java-index,Java Essentials,Java Programming...
# 3
You forgot to escape the leading pipe.
CeciNEstPasUnProgrammeura at 2007-7-13 11:37:39 > top of Java-index,Java Essentials,Java Programming...
# 4

Good you found the comment. Just so others know

/**

* This is how I want to get the substring

* Begin with a '|GS' then at least one '~' then any number or chars followed by only one '|'

* therefore the subsring I hope to get is:

* |GS~PO~LOCALTELSWORDT ~ORDRLS90~20060201~113744~000000001~X~004050|

*/

doopsterusa at 2007-7-13 11:37:39 > top of Java-index,Java Essentials,Java Programming...
# 5
I sorry I forgot to type that in when I wrote my main so you all could test it. it's not that I can't get the begining of the string correct but it won't terminate when it finds the first '|" after the "|GS"Please help
doopsterusa at 2007-7-13 11:37:39 > top of Java-index,Java Essentials,Java Programming...
# 6
I found the problem - . surely includes the pipe as well. I'm trying to find a solution... you don't want "everything" in the middle but "everything but a pipe".
CeciNEstPasUnProgrammeura at 2007-7-13 11:37:39 > top of Java-index,Java Essentials,Java Programming...
# 7
String GSregex = "\\|GS~+[^|]*\\|{1}";
CeciNEstPasUnProgrammeura at 2007-7-13 11:37:39 > top of Java-index,Java Essentials,Java Programming...
# 8
Nope, doesn't work on ||...
CeciNEstPasUnProgrammeura at 2007-7-13 11:37:39 > top of Java-index,Java Essentials,Java Programming...
# 9
Doh, that makes sense.CeCi you are the STUFF!!!! Thanks a million!!!!!!
doopsterusa at 2007-7-13 11:37:39 > top of Java-index,Java Essentials,Java Programming...
# 10
String GSregex = "\\|GS~+([^|]|\\|{2,})*\\|{1}";Maybe there's a simpler way to express "not 1 pipe" instead of "anything but a pipe, or two or more pipes".Now give me all your dukes.
CeciNEstPasUnProgrammeura at 2007-7-13 11:37:39 > top of Java-index,Java Essentials,Java Programming...
# 11

> Maybe there's a simpler way to express "not 1 pipe"

> instead of "anything but a pipe, or two or more pipes".

That's about as simple as it gets, but is that what the OP meant? It sounds to me like he just wants the result to start and end with a pipe, with no pipes in between. In that case, this will work just fine: String GSregex = "\\|GS~.*?\\|";

By the way, "{1}" is pure noise; you're just telling it to do what it would have done anyway if you hadn't added a quantifier.

uncle_alicea at 2007-7-13 11:37:39 > top of Java-index,Java Essentials,Java Programming...
# 12

> That's about as simple as it gets, but is that what

> the OP meant? It sounds to me like he just wants the

> result to start and end with a pipe, with no pipes in

> between.

>> or chars followed by only one '|'

> By the way, "{1}" is pure

> noise; you're just telling it to do what it would

> have done anyway if you hadn't added a quantifier.

Are you sure? Doesn't it mean "exactly one instance, without another one following"?

CeciNEstPasUnProgrammeura at 2007-7-13 11:37:39 > top of Java-index,Java Essentials,Java Programming...
# 13

> Are you sure? Doesn't it mean "exactly one instance,

> without another one following"?

No, for that you would have to use a lookahead: "\\|(?!\\|)"

Your regex works because the greedy star gobbles up all the multiple-pipe sequences, so if the next character is a pipe, it has to be a singleton. However, if the regex engine reaches the end of the input without finding a lone pipe, you could get an incorrect match due to backtracking. For example, given the string "|GS~foo||bar"

the subexpression "([^|]|\\|{2,})*" will initially match "foo||bar", leaving "\\|{1}" with nothing to match. So the regex engine will backtrack until it gets to the position before the two pipes, and "\\|{1}" will match the first one. To prevent that, all you have to do is change the normal, greedy star to a possessive star, thus disabling backtracking: String GSregex = "\\|GS~+([^|]|\\|{2,})*+\\|";

Now it will correctly fail to match my test string.

uncle_alicea at 2007-7-13 11:37:39 > top of Java-index,Java Essentials,Java Programming...