Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Replace words

Last post 11-07-2007, 8:38 PM by ddrudik. 5 replies.
Sort Posts: Previous Next
  •  11-06-2007, 9:03 AM 36290

    Replace words

    Hi,

    Here's my text:
    "Between us there was, as Ms. Niceguy I have already said some-
    where, the bond of the sea.  Besides holding U.S.A. our hearts
    together Ms. Jones through N.A.S.A. long periods of separation, it had the
    effect of making us tolerant of each other's yarns--and
    even Ms. Morizaki convictions."

    I'd like to replace strings like Ms. Niceguy, Ms. Jones, Ms. Morizaki and other following pattern "Ms. Xxx" and this two abbreviations "U.S.A." and "N.A.S.A." with word "ex#" in one regex. Is this possible?

    Wanted result:
    "Between us there was, as ex# I have already said some-
    where, the bond of the sea.  Besides holding ex# our hearts
    together ex# through ex# long periods of separation, it had the
    effect of making us tolerant of each other's yarns--and
    even ex# convictions."

    Thanks for any help!

    // Language: Java (Eclipse)
    // OS: Windows XP SP2

  •  11-06-2007, 11:02 AM 36291 in reply to 36290

    Re: Replace words

    try regex replace with pattern

    (\b[A-Z]\.)+|(Ms\.\x20\S+)

    with replacement string

    ex#


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  11-07-2007, 2:52 PM 36317 in reply to 36291

    Re: Replace words

    Thanks, it works perfect! :)

    But I have another question:
    String input = ReadFromFile("file.txt");
    String replacement = "#ex#";
    String exPtrn = "(\\b[A-Z]\\.)+|(Ms\\.\\x20\\S+)";
    ArrayList<String> exList = new ArrayList<String>();

    I'd like to replace words in input string using exPtrn and put replaced words into exList.
    I tried input = input.replaceAll(exPtrn, replacement), but I don't know how to put these words into collection right away, processing the file only once. Any ideas ?

    I'd appreciate any help!

  •  11-07-2007, 4:34 PM 36318 in reply to 36317

    Re: Replace words

    Unfortunately replaceAll doesn't create a match collection, you would need to use java.util.regex.Matcher for that, but once you have file read into the "input" variable, you would only need to process the "input" variable twice.


  •  11-07-2007, 7:14 PM 36328 in reply to 36318

    Re: Replace words

    So here's my code:

    String input = ReadFromFile("file.txt");
    String replacement = "#ex#";
    String exPtrn = "(\\b[A-Z]\\.)+|(Ms\\.\\x20\\S+)";
    ArrayList<String> exList = new ArrayList<String>();

    Pattern regex = Pattern.compile(exPtrn, Pattern.CANON_EQ);
    Matcher regexMatcher = regex.matcher(input);
    String tmp = new String();

        while (regexMatcher.find()) {
            tmp = regexMatcher.group();
            exList.add(tmp);
    }

    It seems to be working good, but how can I replace found words with "#ex#" in input String.. This code only add exclusions into exList :(  Please help!

  •  11-07-2007, 8:38 PM 36332 in reply to 36328

    Re: Replace words

    Can't say I know Java syntax well, but adding something like this to the bottom your code should work:

    input=input.replaceAll(exPtrn, replacement);

    It's a 2-step process if you want to match and replace.


View as RSS news feed in XML