Replacing a sequence of characters

Hello,
I'm quite surprise (but I am sure that I miss something) that JAVA doesn't give us the equivalent of the function " ereg_replace " in PHP.
Thus, i ask if there is any equivalent method as "replace" but for a String variable and
not for a Char ?
By example, i wish to replace in the following sequence of characters " 256; 134;; 45; 56;; 33; 33; 36; 98; 203;; 57 " each of 2 semicolons which follow each other By "; 0; ".
I can use " indexOf ", combined with setCharAt (via StringBuffer) for example, but that annoys me to do this, because I have the impression to miss something.
Thanks in advance.
JP.

I'm quite surprise (but I am sure that I miss
something) that JAVA doesn't give us the equivalent of
the function " ereg_replace " in PHP.Java is not a domain-specific language (as PHP is) and so it cannot (and shouldn't) afford to include all the possible domain-specific functions into the language.
Such functionality is usually added by libraries, either standard (java.* and javax.* packages) or 3-d party.
For regex functions you can choose from:
- java.util.regex.* package, since JDK 1.4
- jregex.* package, 3-d party, see http://jregex.sourceforge.net
- other 3-d party packages, like jakarta-oro, gnu.regexp, etc.
I can use " indexOf ", combined with setCharAt (via
StringBuffer) Too burdensome. Use regular expressions.

Similar Messages

  • Replacing a set of characters from a string in oracle sql query

    I want to replace a set of characters ( ~    !     @    #     $     %     ^     *     /     \     +    :    ;    |     <     >     ?    _  ,) from a STRING in sql
    select 'TESTING ! ABC 123 #'
    FROM DUAL;
    What is the best way to do it? Please provide examples also.

    What is your expected output... The query I posted just removes them, it never replaces them with spaces..your string already has space.. if you want to remove space as well .. try this...
    SELECT TRANSLATE ('TESTING ! ABC 123 #', '-~!@#$%^*/\+:;|<>?_, ', ' ') FROM DUAL;
    Output:
    --TESTINGABC123
    Else post your expected output..
    Cheers,
    Manik.

  • Word Replacements for Non- English Characters

    Hi
    Does anyone have an idea on implementing Word Replacements for non- english characters in TCA- DQM 11i.
    We are trying to identify, capture and cleanse common accented characters like à, â , ê
    However, the default language for replacement is American English , So even if we add these in the existing lists it will not take any effect
    Is creating a new Word replacement list for every language the solution ?? any patch recommendations???
    Thanks in advance

    It seems that this is an issue that has popped up in various forums before, here's one example from last year:
    http://forum.java.sun.com/thread.jspa?forumID=16&threadID=490722
    This entry has some suggestions for handling mnemonics in resource bundles, and they would take care of translated mnemonics - as long as the translated values are restricted to the values contained in the VK_XXX keycodes.
    And since those values are basically the English (ASCII) character set + a bunch of function keys, it doesn't solve the original problem - how to specify mnemonics that are not part of the English character set. The more I look at this I don't really understand the reason for making setMnemonic (char mnemonic) obsolete and making setMnemonic (int mnemonic) the default. If anything this has made the method more difficult to use.
    I also don't understand the statement in the API about setMnemonic (char mnemonic):
    "This method is only designed to handle character values which fall between 'a' and 'z' or 'A' and 'Z'."
    If the type is "char", why would the character values be restricted to values between 'a' and 'z' or 'A' and 'Z'? I understand the need for the value to be restricted to one keystroke (eliminating the possibility of using ideographic characters), but why make it impossible to use all the Latin-1 and Latin-2 characters, for instance? (and is that in fact the case?) It is established practice on other platforms to be able to use characters such as '�', '�' and '�', for instance.
    And if changes were made, why not enable the simple way of specifying a mnemonic that other platforms have implemented, by adding an '&' in front of the character?
    Sorry if this disintegrated into a rant - didn't mean to... :-) I'm sure there must be good reasons for the changes, would love to understand them.

  • Replacing any non english Characters

    How can I Replace any non english characters I have alot of the characters that look like a block.
    --John                                                                                                                                                                                                                   

    Probably the easiest way to code would be to convert the string to a byte array and back again using the ASCII character encoding. That should give you ? for any non ASCII characters.
    Something like;
    String newString = new String(oldString.getBytes("ASCII"), "ASCII");

  • Replace Microsoft Word strange characters

    I'm trying to create a function to replace all the strange
    characters that come from Microsoft Word, but it doesn't seem to
    work. Any thoughts? Thanks!
    function replaceWordCharacters(str) {
    str = replace(str, chr(8217), chr(39), "all");
    str = replace(str, chr(8216), chr(39), "all");
    str = replace(str, chr(8220), chr(34), "all");
    str = replace(str, chr(8221), chr(34), "all");
    str = replace(str, chr(8211), "-", "all");
    str = replace(str, chr(13), "<br/>", "all");
    WriteOutput(str);
    return str;
    Eric

    Check out this UDF:
    http://www.cflib.org/udf.cfm?ID=725

  • Replacing multiple new line characters

    how to replace multiple new line characters in a line.
    Suppose for example my line contains Example1#####Example2##Example3###Example4,
    then the output should be Example1#Example2#Example3#Example4.

    Hi Sid,
    You Can try this code
    DATA: ld_string TYPE string,
          ld_string1 TYPE string,
          ld_string2 TYPE string,
          ld_string3 TYPE string.
    ld_string = 'Example1#####Example2##Example3###Example4'.
    REPLACE ALL OCCURRENCES OF '######' in ld_string with '#'.
    REPLACE ALL OCCURRENCES OF '####' in ld_string with '#'.
    REPLACE ALL OCCURRENCES OF '###' in ld_string with '#'.
    REPLACE ALL OCCURRENCES OF '##' in ld_string with '#'.
    write:/1 ld_string.
    Regards
    Pavan

  • In Pages, how to search and replace text involving invisible characters?

    In Pages documents, how to search and replace text involving invisible characters, colors and font sizes—a task which is so easy in Mircosoft Word?

    I read that an older version of Pages allowed users to enter special characters in the search/replace fields, but this did not work for me.
    Here: http://www.macworld.com/article/1156533/pagesspecialcharacters.html
    I still am looking for a way to do this.

  • Need help in using replace function with special characters

    I have a column in a table where the data can contain ascii code for special characters such as an apostrophe.
    The data looks like this:
    CREEK&#39;S LANE
    ie for a street named CREEK'S LANE.
    I want to replace the ascii representation with the apostrophe and have the returned data show up as: CREEK's LANE
    When I try the query below I get prompted for substitution variable value.
    I don't seem to be able to find the right syntax to make this query work.
    SELECT REPLACE (street_name, '&#39;', '''')
    FROM
    streets WHERE street_id = 1
    Does anybody know how to do this?
    Any help would be much appreciated.
    Thanks.
    George

    george91 wrote:
    I have a column in a table where the data can contain ascii code for special characters such as an apostrophe.
    The data looks like this:
    CREEK'S LANE
    ie for a street named CREEK'S LANE.
    I want to replace the ascii representation with the apostrophe and have the returned data show up as: CREEK's LANE
    When I try the query below I get prompted for substitution variable value.
    I don't seem to be able to find the right syntax to make this query work.
    SELECT REPLACE (street_name, ''', '''')
    FROM
    streets WHERE street_id = 1
    Does anybody know how to do this?
    Any help would be much appreciated.
    Thanks.
    GeorgeHa! The codes you specified rendered in the HTML, but showed properly when I listed your original posting above. I didn't understand what you meant initially because the 5-character string represenation got rendered as the quote that you said you weren't able to get - a display problem.
    You're getting prompted for the substituon variable because of the ampersand; you appear to be doing this in SQL*PLUS. The first thing I would try is to SET DEFINE OFF when using the ampersands to see if that works. If That doesn't work check the docs to delmit the ampersand (I think its a backslash before it but can't remember offhand). Another, harder option might be to use the TRANSLATE function replacing the literal character instead of using REPLACE (though replacing a quote will be a little tricky). If you're on 10g also consider using the advanced quoting
    Good luck!
    Edited by: riedelme on May 22, 2009 12:45 PM

  • Replacing all occurrences of characters in a string with one new character?

    Hi there,
    I'm looking for a way that I could replace all occurrences of a set of specific characters with just one new character for each occurrence.
    For example if I have a string which is "word1...word2.......word3....word4............word5" how would I be able to replace this with just one character such as ":" for each set of "." so that it would essentially appear like this "word1:word2:word3:word4:word5"
    If I just use replace(".", ":") I am left with "word1:::word2:::::::word4::::word5::::::::::::"
    So therefore I'm guessing this would require the use of replaceAll() maybe? but I'm not really very familiar with how regular expressions work and how this would be accomplished using them.
    Any help would be greatly appreciated :) Thanks.

    Yes, but "." means any character, so ".\+" means "any character repeated more than once". If you just mean a full stop ("period" for you Americans :p) you should use "\.+" as your regular expression, but remember that for Java you need a '\' escape to recognise '\' as '\', so in code you'd actually type your regex as:
    "\\.+"Here's an example of one way to do it:
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class Test {
        public static void main(String[] args) {
           String string = "example1.......example2...example3.....example4";
         Pattern pattern = Pattern.compile("\\.+");
         Matcher stringMatcher = pattern.matcher(string);
         string = stringMatcher.replaceAll(":");
         System.out.println(string);
    }Edited by: JohnGraham on Oct 24, 2008 5:19 AM
    Edited by: JohnGraham on Oct 24, 2008 5:22 AM

  • Reading sequence of characters in Java-----HELP!!!

    I need help with this program here..Im new to Java and struggling to get this to work..here's the problem..
    The program monitors a possibly infinite stream of characters from the keyboard (standard input). If it detects the sequence "www" it outputs a "0". If it detects the sequence "wow" it outputs a "1". DO NOT detect sequences within sequences. The program should exit cleanly when it detects an End Of Input. For example:
    The following sequence wwowowwwowww<End Of Input> would produce the following result: 100
    While the following sequence wwwowowwwwoowowow<End Of Input> would produce the following result: 0101

    If you want to catch each key input realtime, you should use KeyEvent in a Swing/AWT GUI program. Capture each triplet in a small char array and compare it against "www" or "wow".
    Here's a simple code that shows the principle:
    public class Exl5{
      public static void main(String[] args){
        String s1 = "wwowowwwowww";
        String s2 = "wwwowowwwwoowowow";
        System.out.println(convert(s1));
        System.out.println(convert(s2));
      static String convert(String s){
        StringBuffer sb = new StringBuffer();
        int p = 0;
        while (p < s.length() - 2){
          String ss = s.substring(p, p + 3);
          if (ss.equals("wow")){
            sb.append("1");
            p += 3;
          else if (ss.equals("www")){
            sb.append("0");
            p += 3;
          else{
            ++p;
        return new String(sb);
    }

  • Java.lang.String:   How can we replace the list of characters in a String?

    String s = "abc$d!efg:i";
    s = s.replace('=', ' ');
    s = s.replace(':', ' ');
    s = s.replace('$', ' ');
    s = s.replace('!', ' ');
    I want to check a list of illegal characters(Ex: $ = : ! ) present in the String 's' and replace all the occurance with empty space ' '
    I feel difficult to follow the above code style.. Because, If list of illegal characters increased, need to add the code again to check & replace the respective illegal character.
    Can we refactor the above code with any other simple way?
    Is there any other way to use Map/Set in this above example?

    RTFM
    http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html
    That document is almost perfectly useless for someone
    who is just starting to learn regexes. What they
    need is a good tutorial, and the best one I know of
    for Java programmers is the one at
    this site. When you're ready to move on from
    introductory tutorials, get the latest edition of
    Mastering Regular Expressions, by Jeffrey Friedl.Dear Uncle,
    maybe you want to read that document first before judging its usefulness. And just in case you can't be bothered, the following is an exact quote, which accidently happens to clearly explain the op's question on the usage of \\[\\] in sabre's regex:
    Backslashes, escapes, and quoting
    The backslash character ('\') serves to introduce escaped constructs, as defined in the table above, as well as to quote characters that otherwise would be interpreted as unescaped constructs. Thus the expression \\ matches a single backslash and \{ matches a left brace.
    It is an error to use a backslash prior to any alphabetic character that does not denote an escaped construct; these are reserved for future extensions to the regular-expression language. A backslash may be used prior to a non-alphabetic character regardless of whether that character is part of an unescaped construct.
    Backslashes within string literals in Java source code are interpreted as required by the Java Language Specification as either Unicode escapes or other character escapes. It is therefore necessary to double backslashes in string literals that represent regular expressions to protect them from interpretation by the Java bytecode compiler. The string literal "\b", for example, matches a single backspace character when interpreted as a regular expression, while "\\b" matches a word boundary. The string literal "\(hello\)" is illegal and leads to a compile-time error; in order to match the string (hello) the string literal "\\(hello\\)" must be used.

  • SQL Replace function: errors if replacement text is 8191 characters

    Oracle 10.2.0.3.0 Enterprise Edition
    I'm working on an application which has a function to generate emails.
    We have a template, which gets filled with data from the database depending on the email being sent.
    To do this I'm using the Replace function to replace certain parts of the template with the right data.
    It seems that if the replacement text (the third argument to the function) is longer than 8191 bytes the function throws a PL/SQL: numeric or value error.
    This limitation doesn't appear to be documented anywhere, certainly nowhere that I can find.
    Anyone know of a way around this short of building my own replace function?

    I've figured out what was going on.
    The database column for the replacement text is defined as a clob, but there is a front end limit of 10,000 characters for the field being used.
    When using the replace function I was assigning the value to a varchar2(32767) field in the PL/SQL.
    So in terms of datatypes the function call is:
    varchar2 := replace(varchar2, varchar2, clob)
    If the length of the clob is over 8191 characters it errors.
    (Presumably because of the bug mentioned in this thread DBMS_LOB.SUBSTR() returning 8191 characters instead of 32767
    I've fixed it by simply changing the datatype of the variable to which the value is being assigned to a clob.

  • UDF for replacing special or junk characters

    Hi
    Is there any stanard function for replacing any junk characters with zero? If not, I needed UDF for fulfilling this requirement.
    My requirment is .... Am sending some files to r/3 using XI. In the source(txt file) some time some fields have some junk characters. I must make it to zero, the field which has junk. How can I acheive it? is there any UDF using which I can fulfill my requiremnt.
    Thanks.

    Hi,
    Use this UDF
    Here a is the input value.
    String b = "";
    b = a.replaceAll(",", "0");
    b = a.replaceAll("@", "0");
    Like all the special characters you can take and replace with zero.
    return b;
    Or
    Use the regular expression to check whether special characters occurs in the string then replace with zero
    http://www.j2ee.me/developer/technicalArticles/releases/1.4regex/
    Regards
    Ramesh.

  • Replacing windows new line characters with JAVA

    I am importing a file into the database... the file is written by a client using windows, and it is a text file.
    When there is a windows new line character at the end of the line (^M), it is getting saved to the database as part of the string value.
    When we parse this data to run a report, the (^M) are being written back out to the file and causing the fields and data not to line up.
    We want to REPLACE the ^M with some other character (whatever it doesn't matter)... how can java do that???
    We tried replacing '/n' with '###' and it will show the ###, but the line still breaks.
    TIA,
    Tonya

    The characters in question are cr = \r = ascii(13) and lf = \n = ascii(10). Windows uses CrLf, Unix uses Lf and some systems use just Cr. You can use the String.replaceAll or the pattern matching classes to fix this up, see java.util.regex.Pattern, and java.util.regex.Matcher.

  • Find/Replace Extended Character Set characters in filenames in one pipeline

    Hello all,
    I have to work with some very bored people. Instead of putting a dash (hex 2d) into a filename, they opt for something from this
    set of extended characters, which makes my regular expressions fail.  IS there a way I can efficiently find & replace anything outside the standard character set
    in one pipelinewithout finding and replacing a character at a time?
    So,I'd like something like:
    get-childitem * | where-object $_.name -match '\x99' | rename-item -newname { $_.name -replace '\x99','='}
    from hex 80 to hex FF rather than a for-each.
    Thanks.

    Answer would depend on the way you want to replace... Easier if you want replace any char in set with selected char:
    $Name = -join (180..190|%{[char]$_})
    New-Item -ItemType File -Name $Name
    Get-ChildItem * | Rename-Item -NewName {
    [regex]::Replace(
    $_.Name,
    '[\xB4-\xBE]',
    } -WhatIf
    But if you want it more complicated, you may do that too. E.g. defining hashtable that can be used to replace individual elements:
    $Replacer = @{}
    foreach ($Char in (180..190 | % { [char]$_ })) {
    $Replacer.Add(
    [string]$Char,
    (echo _, -, =, . | Get-Random)
    $Replacer
    Get-ChildItem * | Rename-Item -NewName {
    [regex]::Replace(
    $_.Name,
    '[\xB4-\xBE]',
    $Replacer[$args[0].Value]
    } -WhatIf
    Using this syntax make it possible to include some logic in replace. E.g. you could easily use switch to decide what to do with given string:
    Get-ChildItem * | Rename-Item -NewName {
    [regex]::Replace(
    $_.Name,
    '[\xB4-\xBE]',
    switch ($args[0].Value) {
    º { "0" }
    µ { "u" }
    ¹ { "1" }
    ¸ { "," }
    Default { "_" }
    } -WhatIf

Maybe you are looking for