Want to replace a string containing consecutive repeating  words to one using regular expression

Hi Experts,
I need a regular expression to replace all duplicate words in a string with one.
eg: 'Hello Hello World 4-4-5 etc etc' should be changed to 'Hello World 4-4-5 etc'.
I tried many of them but they had one or the other problem. like (\w+\S\W)\1+' replace with ' \1' and  ' (\w+\W)\1+' replaced with ' \1' , etc
Thanks in advance
Tarique

Hi,
Translating what frank said to JAVA would be something like this:
        StringBuffer result = new StringBuffer();
        String myString = "This is right right, that is wrong.";
        String[] words = myString.split(" ");
        String lastWord = "";
        for (String str : words){
            if (!str.contains(lastWord))
                result.append(str);
            else
                result.append(str.substring((lastWord.length() >= 0 ? lastWord.length() : 0 ) , str.length()));
            lastWord = str;
            result.append(" ");
        System.out.println(result);
If you didnt have points and commas in your message then would be easier. But the code is not 100% correct and you will need to make it work according to yours requirements.

Similar Messages

  • Using Regular Expressions to replace Quotes in Strings

    I am writing a program that generates Java files and there are Strings that are used that contain Quotes. I want to use regular expressions to replace " with \" when it is written to the file. The code I was trying to use was:
    String temp = "\"Hello\" i am a \"variable\"";
    temp = temp.replaceAll("\"","\\\\\"");
    however, this does not work and when i print out the code to the file the resulting code appears as:
    String someVar = ""Hello" i am a "variable"";
    and not as:
    String someVar = "\"Hello\" i am a \"variable\"";
    I am assumming my regular expression is wrong. If it is, could someone explain to me how to fix it so that it will work?
    Thanks in advance.

    Thanks, appearently I'm just doing something weird that I just need to look at a little bit harder.

  • How do I know if I have an extended warranty? I purchased Now 9, 2011 almost 2 years. I dropped it this morning and broke the screen. I want to replace it, do I still get a new one?

    how do I know if I have an extended warranty? I purchased Now 9, 2011 almost 2 years. I dropped it this morning and broke the screen. I want to replace it, do I still get a new one?

    hudsonclub wrote:
    how do I know if I have an extended warranty?
    Enter your iPad's serial number in the following page: https://selfsolve.apple.com/agreementWarrantyDynamic.do
    Your warranty and service options will appear. If you purchased AppleCare+ (AppleCare Plus) you may be in luck.
    If you do not know its serial number you can find it stored in your last iTunes backup:
    Open iTunes on the computer you used to sync the iPad and select Preferences…from the iTunes menu. (In Windows, choose Edit > Preferences.)
    Select the Devices pane. Position the cursor over any of its backups. The iPad's serial number will appear in a popup window.
    For a screenshot read: iOS: How to find the serial number, IMEI, MEID, CDN, and ICCID number
    Scroll down to:
    Additional Information >
    Getting the information from your computer >
    If you have access to your computer, but not your device

  • Changeparticular characters in a string by using regular expressions ...

    Hello Everyone,
    I am trying to write a function by using oracles regular expression function REGEXP_REPLACE but I could not succed till now.
    My problem as follows, I have a text in a column for example let say 'sdfsdf Sdfdfs Sdfd' I want replace all s and S characters with X and make the text look like 'XdfXdf XdfdfX Xdfd'.
    Is it possible by using regular expressions in oracle ?
    Can you give me some clues ?
    Thank you

    SSU wrote:
    Hello Everyone,
    I am trying to write a function by using oracles regular expression function REGEXP_REPLACE but I could not succed till now.
    My problem as follows, I have a text in a column for example let say 'sdfsdf Sdfdfs Sdfd' I want replace all s and S characters with X and make the text look like 'XdfXdf XdfdfX Xdfd'.
    Is it possible by using regular expressions in oracle ?
    Can you give me some clues ?
    Thank you
    SQL> SELECT
      2  regexp_replace('sdfsdf Sdfdfs Sdfd','s|S','X') from dual;
    REGEXP_REPLACE('SD
    XdfXdf XdfdfX XdfdRegards,
    Achyut

  • Format string using Regular Expression

    Input string output format...
    SELECT q'<select ab_c "ABC", efg "EFG" from dual>' str FROM DUAL
    Output:
    STR                                 
    select ab_c "ABC", efg "EFG" from dual
    Required output format using regular expression...
    STR                                 
    select 'ab_c' "ABC", 'efg' "EFG" from dual

    Regular expressions have many limitations as parsing tools, and you didn't specify the rules you wanted. This expression puts quotes around the non blank string before a quoted string:
    SELECT regexp_replace(q'<select ab_c "ABC", efg "EFG" from dual>',
                          '([^" ]+)( +"[^ ]*")' , '''\1''\2' ) str FROM DUAL;
    STR
    select 'ab_c' "ABC", 'efg' "EFG" from dual
    {code}
    It is not robust - a missing " will confuse it, and you should be using bind variables anyway.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • String extract using regular expression

    Hi
    I have text like this "<a>45</a><ct>Hi</ct><R>45 85</R><H>Here</H>" .I want to extract using regular expression or any techniques the text between <R> and </R> also need to replace the space with pipe between 45 and 85 like "45|85"
    Edited by: vishnu prakash on Mar 2, 2012 4:42 AM

    Hi,
    Here's one way:
    REPLACE ( REGEXP_REPLACE ( txt
                    , '.*<R>(.*)</R>.*'
                    , '\1'
         , '|'
         )This assumes there is only one <R> tag in txt.
    Always say which version of Oracle you're using. The expression above will work in Oralce 10 and up, but starting in Oracle 11 you can use REGEXP_SUBSTR rather than the less intuitive REGEXP_REPLACE.
    Edited by: Frank Kulash on Mar 2, 2012 7:48 AM

  • Find/Replace Using Regular Expressions

    Can someone help me with this...I am using Regular expressions to
    FIND:
    http.*lid=([^&"]*)[^"]*
    REPLACE:
    $set(\1,ID_id,code)$
    So that in the following it will change this:
    a href="http://www.test.com/shc/s/home_10153_12605?lid=Search" rilt="Search"
    To this:
    a href="$set(Search,ID_id,code)$" rilt="Search
    Those expressions  work in Notepad++ but when i use dreamweaver it just replaces the http... with "$set(\1,ID_id,code)$" and doesnt reference the "search"
    Any help?
    Thanks

    Let me begin by saying I'm a complete idiot with DW's Reg Ex.   I use Search Specific Tag whenever possible.  See screenshot below.
    Try this on your Current Document to see if it works. Then make a back-up copy of site before attempting it on Entire Local Site as you cannot "Undo" this process.
    Good luck,
    Nancy O.

  • Spliting a large string using regular expression which contain special char

    I have huge sting(xml) containing normal character a-z,A-Z and 0-9 as well as special char( <,>,?,&,',",;,/ etc.)
    I need to split this sting where it ends with </document>
    for e.g.
    Original String:
    <document>
    <item>sdf</item>
    <item><text>sd</text</item>
    </document>
    <document>hi</document>
    The above sting has to be splited in to two parts since it is having two document tag.
    Can any body help me to resolve this issue. I can use StringTokenizer,String split method or Regular expression api too.

    manas589 wrote:
    I used DOM and sax parser and got few exception. Again i don't have right to change xml. so i thought to go with RegularExpression or some other way where i can do my job.If the file actually comes in lines like what you posted, you should just be able to compare the contents of each line to see if it contains "</document>" or whatever you're looking for. I wouldn't use regex unless I needed another problem.
    I got excpetion like: Caused by: org.xml.sax.SAXParseException: The entity "nbsp" was referenced, but not declared.
         at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
         at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
         at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)So then it isn't even XML.
    Edit: sorry, I just realized why you're considering all of these heavy-duty ideas. It's just that you don't know how to break the string into lines. You do it like this:
    BufferedReader  br = new BufferedReader(new StringReader(theNotXMLString));

  • Replace a string using regular expression from powershell

    I want to replace the following:
    'browserName': 'firefox'
    with :
    'browserName': 'chrome'
    then I tried this:
    (get-content $conffile) -replace "^('browserName': ')\S+","browserName': 'chrome' |set-content $conffile
    But nothing happened.
    Could someboby tell me how to write the regular expression here? Thanks a lot.

    Second person today with the same question.
    get-content $conffile |%{$_ -replace "'browserName':\s+'firefox'","'browserName': 'chrome'"  | set-content $conffile
    \_(ツ)_/

  • String replace using regular expressions

    I'm not very good at regular expressions, but I would like my script to replace
         <a href="somepage.html">
    by
    <a href="event:somepage">
    How do I do this?  Thanks in advance!

    Replacing a string that matches a certain pattern with another string is one of the more common RegEx tasks. There is documentation on using them here:
    http://livedocs.adobe.com/flex/3/html/help.html?content=12_Using_Regular_Expressions_01.ht ml
    hth,
    matt horn
    flex docs

  • Using regular expressions to find and replace code.

    Hi! Semi-newbie coder here.
    I'm trying to strip out code from multiple pages, I've tried regular expressions but I'm struggling to understand them. I also need to do it across a LOT of pages, so I need an automated way of doing it.
    The best way I can explain is with an analogy:
    I want to delete any string of characters that start with c, ends with t and includes anything inbetween, so it would pick up "cat, cut, chat, coconut, can do it" whatever appears in the middle of those.
    Except, instead of c and t, I want it to find strings of code starting with <div class="advert" and ending with Vote<br> while picking up everything in between, (including spaces, code, comments, etc.). Then, deletes that whole string including the starting and ending.Is there a regular expression I could use in dreamweaver that could do this? Is there a way to do this at all?

    Let me begin by saying I'm a complete idiot with DW's Reg Ex.   I use Search Specific Tag whenever possible.  See screenshot below.
    Try this on your Current Document to see if it works. Then make a back-up copy of site before attempting it on Entire Local Site as you cannot "Undo" this process.
    Good luck,
    Nancy O.

  • Filter String using Regular Expression

    Hello,
    I have an application that monitors serial communication between a PC and device.  The message protocol is a byte stream that I convert to a string to parse into pretty messages.  The start of the string is always "10 02", but if the string is preceded with another "10" like this "10 10 02" it is part of a message.  I've been trying to use a regular expression with the Search and Replace VI.  My regex is "[^10]\s10\s02" which almost works but it cuts off part of the message:
    Before:
    10 03 10 02
    After:
    10 0   <= missing the "3"
    10 02
    Here's what I'm doing:
    Any ideas on what I'm missing?  I've attached a simple example.
    Thanks
    Message Edited by Derek Price on 02-14-2008 08:37 PM
    Attachments:
    Filter Beginning Message1.vi ‏14 KB
    FilterMessageRegex1.png ‏7 KB

    Try this approach.
    Do search and replace on '10\s02' and replace with '\r\n10\s20'
    Then do another search and replace on '10\r\n\10\s20'  with '10\s10\s20'
    See attached.
    Randall Pursley
    Attachments:
    Message Filter.PNG ‏18 KB

  • Dumbfounded by Scanner processing String using regular expression

    I was reading Bruce Eckel's book when I came across something interesting: extending Scanner with regular expressions. Unfortunately, I was confronted with an issue that doesn't make much sense to me: if the String that I am scanning contains a hyphen, the Scanner doesn't produce anything. As soon as I take it out, it all works like a charm. Here is my example:
    import java.util.Scanner;
    import java.util.regex.*;
    public class StringScan {
    public static void main (String [] args){
         String input = "there's one caveat when scanning with regular expressions";
         Scanner scanner = new Scanner (input);
         String pattern = "[a-z]\\w+";
         while (scanner.hasNext(pattern)){
              scanner.next(pattern);
              MatchResult match = scanner.match();
              String output = match.group();
              System.out.println(output);
    }What could be the reason? I imagined it could be because the hyphen for some reason gets given a special meaning but when I tried escaping it, it still didn't work.

    Thanks for your prompt reply.
    I have figured out what was wrong with my code, by the way. Since a single quote is not a word character, it does not match w+. And as the very first input token does not match, the scanner stops immediately. I rewrote my regex to "[a-z].*" and now it does work.

  • How to use regular expression replace for this special characters?

    hi,
    I need to replace the below string, but i couldnt able to do if we use the special charaters '+', '$' . can anyone suggest a way to do this?
    select REGEXP_REPLACE('jan + feb 2008','jan + feb 2008', 'feb',1,0,'i') from dual
    anwers should be :- feb

    you should use escape character \.
    the regular expression will look like as follows:
    select REGEXP_REPLACE('jan + feb 2008','jan \+ feb 2008', 'feb',1,0,'i') from dual
    hope this is what you needed.
    cheers,
    Davide

  • GUI Based tool for search and replace using regular expression

    Hi,
    I have developed this small tool which can be used for search and replace in multiple files using reqular expressions.
    Features:
    1. Full regular expression
    2. GUI based with Highlighted results
    3. Preview for replace available
    4. Pure Java based.
    5. Its like unix sed and grep
    Please visit below site for download/more information :
    http://sourceforge.net/projects/regexsearchrepl/
    Thanks,
    Hitesh Viseria

    I agree with you, it cannot compete grep/sed/awk combination. I couldnt find anything even close to grep/sed for windows which will also have a preview and most important, free. That is what made me write this tool. I am trying to
    improve its performance and more features like history etc.
    Any suggestions on additional features are most welcome.

Maybe you are looking for

  • All day event in iCal

    I am trying to add an 'All day event' in iCal (have tried on the MacBook Pro and iPhone4) for the 7/10/11. This keeps saving over 2 days the 6/10 and 7/10, even if I change the date manually, after saving it reverts back to both days. Have done plent

  • HP LaserJet 2820 All-in-One fails to print on right side of the page

    I have HP LaserJet 2820 All-in-One for 1.5 years now and before the first set of cartridges run out of ink, the printed page comes out blur or no printing on the right side of the page. No smear on the right page.  It doesn't matter if I used color o

  • Mapping of recursive item

    Component I am working on consists of two Views. One recursive structure is shared by both of them. Fist View fills the structure and second one displays the structure data in accordance with different filters. I defined global structure in the Compo

  • How to use macbook air as ereader

    I'm interested in a new Macbook Air. I'd like to know how to use it as an ereader.

  • How do I fix duplicate iCloud Movies?

    This problem only applies to movies I have purchased from the iTunes Store.... Every so often, I find that iTunes does not properly register that I have successfully downloaded a movie, and therefore it downloads it again.  On other occasions, iTunes