Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 11-01-2004, 03:00 AM   #1 (permalink)
philthee
Registered User
 
Join Date: Oct 2004
Posts: 15
philthee is on a distinguished road
regex

Hi again,

been struggling to get a simple regex to work.

I want to remove all instances of the period . character from my line but i can't quite figure it out.

from my code

String bandName = toks.nextToken();
String temp = bandName;
Pattern removePeriods = Pattern.compile("\\W"); // i think??
Matcher matcher = removePeriods.matcher(temp);

// as far as i can see that should find the first instance of . (period character)
// if so, how do i substitute it with nothing?
philthee is offline   Reply With Quote
Old 11-01-2004, 03:45 AM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
Regex under Java sucks.

Anyways, playing around with it, I think I discovered how to accomplish your goal. I'm not sure where the '\W' came from, but here's what I did to remove periods:

Code:
    String foo = ".f.o.o.b.a.r.";
    Pattern pattern = Pattern.compile("[\\.]");
    System.out.println(pattern.matcher(foo).replaceAll(""));
__________________
GitS
Belisarius is offline   Reply With Quote
Old 11-01-2004, 04:10 AM   #3 (permalink)
philthee
Registered User
 
Join Date: Oct 2004
Posts: 15
philthee is on a distinguished road
nice one chief!
\\W removes any non word characters i think.
anyhoos, if i want to put the new string back in to my bandName variable how do i do that?

i now have

Code:
String bandName = toks.nextToken();
String temp = bandName;
Pattern removePeriods = Pattern.compile("[\\.]");
System.out.println(removePeriods.matcher(temp).replaceAll (""));
i tried

String bandName = temp;
but netbeans does not like that statement after the other code because "bandName has already been defined in java Main"
philthee is offline   Reply With Quote
Old 11-01-2004, 04:19 AM   #4 (permalink)
philthee
Registered User
 
Join Date: Oct 2004
Posts: 15
philthee is on a distinguished road
actually if i do this then this should be ok i would presume

String bandName = toks.nextToken();

Pattern removePeriods = Pattern.compile("[\\.]");
System.out.println(removePeriods.matcher(bandName) .rep laceAll (""));

no need to put it in to a temp variable and then p8utting it back again
philthee is offline   Reply With Quote
Old 11-01-2004, 04:26 AM   #5 (permalink)
philthee
Registered User
 
Join Date: Oct 2004
Posts: 15
philthee is on a distinguished road
hmmmm, doesn't seem to work, i still just get the periods in bandNAme a few lines of code later when i ask to print.
philthee is offline   Reply With Quote
Old 11-01-2004, 04:34 AM   #6 (permalink)
philthee
Registered User
 
Join Date: Oct 2004
Posts: 15
philthee is on a distinguished road
hahaha
no, i'm speaking crap,
its all cool (i hope)
philthee is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On


Similar Threads
Thread Thread Starter Forum Replies Last Post
regex match help please sde PHP 4 06-24-2003 07:32 PM
preg_replace regex question sde PHP 4 09-18-2002 10:23 AM
regex? sde PHP 4 07-02-2002 04:40 PM


All times are GMT -8. The time now is 07:12 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting