Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Keeping the split token in a Java regular expression

Reply
Thread Tools

Keeping the split token in a Java regular expression

 
 
laredotornado
Guest
Posts: n/a
 
      03-26-2012
Hi,

I'm using Java 6. I want to split a Java string on a regular
expression, but I would like to keep part of the string used to split
in the results. What I have are Strings like

Fri 7:30 PM, Sat 2 PM, Sun 2:30 PM

What I would like to do is split the expression wherever I have an
expression matching /(am|pm),?/i . Hopefully I got that right. In
the above example, I would like the results to be

Fri 7:30 PM
Sat 2 PM
Sun 2:30 PM

But with String.split, the split token is not kept within the
results. How would I write a Java parsing expression to do what I
want?

Thanks, - Dave
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      03-26-2012
laredotornado wrote:
> I'm using Java 6. I want to split a Java string on a regular
> expression, but I would like to keep part of the string used to split
> in the results. What I have are Strings like
>
> Fri 7:30 PM, Sat 2 PM, Sun 2:30 PM
>
> What I would like to do is split the expression wherever I have an
> expression matching /(am|pm),?/i . Hopefully I got that right. In
> the above example, I would like the results to be
>
> Fri 7:30 PM
> Sat 2 PM
> Sun 2:30 PM
>
> But with String.split, the split token is not kept within the
> results. How would I write a Java parsing expression to do what I
> want?


Based on what you've shown it looks like you could split on the comma and trim the resulting strings.

--
Lew
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      03-26-2012
On 03/26/2012 09:22 PM, Lew wrote:
> laredotornado wrote:
>> I'm using Java 6. I want to split a Java string on a regular
>> expression, but I would like to keep part of the string used to split
>> in the results. What I have are Strings like
>>
>> Fri 7:30 PM, Sat 2 PM, Sun 2:30 PM
>>
>> What I would like to do is split the expression wherever I have an
>> expression matching /(am|pm),?/i . Hopefully I got that right. In
>> the above example, I would like the results to be
>>
>> Fri 7:30 PM
>> Sat 2 PM
>> Sun 2:30 PM
>>
>> But with String.split, the split token is not kept within the
>> results. How would I write a Java parsing expression to do what I
>> want?

>
> Based on what you've shown it looks like you could split on the comma and trim the resulting strings.


And one wouldn't even need a regular expression for that.
http://docs.oracle.com/javase/6/docs...Tokenizer.html

Kind regards

robert
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      03-26-2012
On 3/26/2012 11:54 AM, laredotornado wrote:

> Fri 7:30 PM, Sat 2 PM, Sun 2:30 PM
>
> But with String.split, the split token is not kept within the
> results. How would I write a Java parsing expression to do what I
> want?



What Lew said.

String[] dates = dateString.split( ", +" );

for( String date : dates ) {

String temp = date.trim().toUpper();

if( temp.endsWith( "PM" ) ) {
System.out.println( "Good afternoon." );
else if( temp.endsWith( "AM" ) {
System.out.println( "Good morning." );
} else {
System.out.println( "Good whatever." );
}

}

 
Reply With Quote
 
laredotornado@gmail.com
Guest
Posts: n/a
 
      03-26-2012
On Monday, March 26, 2012 1:54:40 PM UTC-5, laredotornado wrote:
> Hi,
>
> I'm using Java 6. I want to split a Java string on a regular
> expression, but I would like to keep part of the string used to split
> in the results. What I have are Strings like
>
> Fri 7:30 PM, Sat 2 PM, Sun 2:30 PM
>
> What I would like to do is split the expression wherever I have an
> expression matching /(am|pm),?/i . Hopefully I got that right. In
> the above example, I would like the results to be
>
> Fri 7:30 PM
> Sat 2 PM
> Sun 2:30 PM
>
> But with String.split, the split token is not kept within the
> results. How would I write a Java parsing expression to do what I
> want?
>
> Thanks, - Dave


Hi, I don't want to split on the comma because there could be a case where the given String is "Fri 8 PM, Sat 1, 3, and 5 PM" and in this case, I want the result to be a String array containing

Fri 8 PM
Sat 1, 3, and 5 PM

Your continued help is appreciated, - Dave
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      03-26-2012
On 3/26/2012 2:21 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Hi, I don't want to split on the comma because there could be a case
> where the given String is "Fri 8 PM, Sat 1, 3, and 5 PM" and in this
> case, I want the result to be a String array containing
>
> Fri 8 PM Sat 1, 3, and 5 PM



You might be able to do this with clever use of regex look-around:

http://www.regular-expressions.info/lookaround.html

Maybe something like "(?<=M),". Definitely take some time to test that
carefully though.

Otherwise, you'll have to write your own parser (which wouldn't be hard).



 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      03-26-2012
On 3/26/2012 2:21 PM, (E-Mail Removed) wrote:
> On Monday, March 26, 2012 1:54:40 PM UTC-5, laredotornado wrote:
>> Hi,
>>
>> I'm using Java 6. I want to split a Java string on a regular
>> expression, but I would like to keep part of the string used to split
>> in the results. What I have are Strings like
>>
>> Fri 7:30 PM, Sat 2 PM, Sun 2:30 PM
>>
>> What I would like to do is split the expression wherever I have an
>> expression matching /(am|pm),?/i . Hopefully I got that right. In
>> the above example, I would like the results to be
>>
>> Fri 7:30 PM
>> Sat 2 PM
>> Sun 2:30 PM
>>
>> But with String.split, the split token is not kept within the
>> results. How would I write a Java parsing expression to do what I
>> want?
>>
>> Thanks, - Dave

>
> Hi, I don't want to split on the comma because there could be a case where the given String is "Fri 8 PM, Sat 1, 3, and 5 PM" and in this case, I want the result to be a String array containing
>
> Fri 8 PM
> Sat 1, 3, and 5 PM
>
> Your continued help is appreciated, - Dave


public class test {
public static void main(String[] args) {
String str = "Fri 7:30 PM, Fri 8 PM, Sat 1, 3, and 5 PM";
String token = "PM, |PM";

String[] strs = str.split(token);
for (String s : strs)
System.out.println(s+"PM");

}
}

C:\Documents and Settings\Knute Johnson>java test
Fri 7:30 PM
Fri 8 PM
Sat 1, 3, and 5 PM

If you wanted to get AMs too, you could do a first pass for the PMs and
then do it again for the AMs.

--

Knute Johnson
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      03-26-2012
On 3/26/2012 3:56 PM, Knute Johnson wrote:

> String str = "Fri 7:30 PM, Fri 8 PM, Sat 1, 3, and 5 PM";

....
> System.out.println(s+"PM");

^^

What does this print if the "str" string ends with AM instead of PM? I
don't think this actually works....

 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      03-26-2012
laredotornado <(E-Mail Removed)> writes:
>What I would like to do is split the expression wherever I have an


public class Main
{
public static void split
( final java.lang.String text )
{ java.util.regex.Pattern pattern =
java.util.regex.Pattern.compile
( ".*?(?:am|pm),?", java.util.regex.Pattern.CASE_INSENSITIVE );
java.util.regex.Matcher matcher = pattern.matcher( text );
while( matcher.find() )
java.lang.System.out.println( matcher.group( 0 )); }

public static void main( final java.lang.String[] args )
{ split( "Fri 7:30 PM, Sat 2 PM, Sun 2:30 PM" ); }}

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      03-26-2012
Stefan Ram wrote:
> laredotornado writes:
>>What I would like to do is split the expression wherever I have an

>
> public class Main
> {
> public static void split
> ( final java.lang.String text )
> { java.util.regex.Pattern pattern =
> java.util.regex.Pattern.compile
> ( ".*?(?:am|pm),?", java.util.regex.Pattern.CASE_INSENSITIVE );
> java.util.regex.Matcher matcher = pattern.matcher( text );
> while( matcher.find() )
> java.lang.System.out.println( matcher.group( 0 )); }
>
> public static void main( final java.lang.String[] args )
> { split( "Fri 7:30 PM, Sat 2 PM, Sun 2:30 PM" ); }}


This excellent (except for layout) example deserves to be archived.

--
Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular expression capture dependent on token order? sthussey@gmail.com Javascript 1 02-09-2007 12:15 AM
Token pasting (## operator) - Add whitespace to a token Wessi C Programming 3 08-11-2005 01:02 PM
"token" "token sequence" "scalar variable" "vector" ?? G Fernandes C Programming 1 02-18-2005 05:32 AM
preprocessor, token concatenation, no valid preprocessor token Cronus C++ 1 07-14-2004 11:10 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments