LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 04-13-2006, 07:19 AM   #1
linuxmandrake
Member
 
Registered: Sep 2004
Distribution: debian sarge 64bit (AMD)
Posts: 709

Rep: Reputation: 30
Rular expression matching with java


String x = s.replaceAll("[a-zA-Z]",s);
public static void main(String[] args) {
Highscores high = new Highscores();

System.out.println(high.process("Steven 500"));

}
gives me Steven 500Steven 500Steven 500Steven 500Steven 500Steven 500 500
why? According to http://java.sun.com/developer/techni...ases/1.4regex/ my regular expression is valid
 
Old 04-13-2006, 11:35 AM   #2
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
well since you omitted some code i am not sure what HighScores.process() does.. however i would guess that you are replacing every letter (you have 6 of them) with the string 'Steven 500' (which is printed 6 times)..
 
Old 04-13-2006, 11:35 AM   #3
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 39
It looks like you want to do a regex-powered replacement on some string, right? This code using java.util.regex works for me:
Code:
import java.util.regex.*;

public class StringMatch {
    public static void main(String[] args) {
        Pattern p = Pattern.compile("foo");
        for (int i = 0; i < args.length; i += 1) {
            System.out.println(p.matcher(args[i]).replaceAll("bar"));
        }
    }
}
It goes through each argument and replaces each occurrence of "foo" with "bar".

First you have to create a regex Pattern object using the Pattern class' static compile() method. Then call p.matcher(String) to get a Matcher object from that pattern, ready to process whatever string you pass as an argument. Then you can call a function on the Matcher, which does whatever you want. replaceAll(String) does a substitution and returns a new string. matches() just returns true or false. You can even get grouped-match results using the group(int) function.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find/grep command to find matching files, print filename, then print matching content stefanlasiewski Programming 7 01-06-2008 02:10 AM
regular expression matching linuxmandrake Programming 2 03-16-2006 06:00 AM
regular expresions: java: matching lines with out string zymos Programming 1 05-28-2005 09:17 AM
Matching with SQL Ephracis Programming 4 12-16-2004 05:09 AM
Problems with matching alitrix Programming 3 11-18-2003 07:24 PM


All times are GMT -5. The time now is 05:16 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration