Regex to replace all occurrences of an empty string, except the last

Hello, everyone!

I need get to a regular expression to replace all occurrences of an empty string except the last. Right now I'm doing this with this code:
public static void main(String[] args)
{
    String str = "abc";
    String regex = "";
    System.out.println(str.replaceAll(regex, "-").replaceFirst("-$", "")); // -a-b-c
}
As you can see, to do this, I have to make two calls: one for replaceAll and another for replaceFirst to get rid of the last character. So I would like to get rid of the call to replaceFirst and have the variable of regex as being a regular expression that would be to insert the character along the chain of all but the last. I'm really not very good with regexs, so I ask your help here. The thing I work, but I think it would be more elegant with just a regex to do.

Thank you in advance.

Marcos

Try this:

String data = "abc";
System.out.println(data.replaceAll(".", "-$0"));

Tags: Java

Similar Questions

  • Find and replace all occurrences of a string within the current paragraph.

    I did so much work, and it's my last method to implement.

    I want to get the current active doc the current paragraph and replace all occurrences of a string inside this paragraph with another string.

    For example, say I have the following paragraph in a document:

    «This is a sentence that.» This is another sentence. It is also another sentence. The sentence ".

    If the user opens the document containing the paragraph and goes to this paragraph by clicking in any place in there, I want to replace e.g.g "another punishment" with "phrase.

    But only in the current active paragraph in the active document.

    I found a code with F_ApiFind and F_ApiGetTextRange to get the current text selection, but I can't think of a way to do it.

    Post edited by: Efstathios Chatzikyriakidis

    Problem solved people. Thank you.

  • I did several back ups on my computer and I wonder how many of the discs that I have to hold. Can I get rid of all except the last backup?

    I did several back ups on my computer and I wonder how many of the discs that I have to hold. Can I get rid of all except the last backup?

    Thank you

    Scott

    Hi Scott,.

    You must keep all 10 backup.the disk new backups will contain only the last backups and will not have the previous backups.

  • How to hide all instances of subform (via a click event of the button) except the last instance.

    When my form is rendered, it can contain multiple 'case' of a subform full page.  So basically, I have a subform labeled "Info" that spans a full page. Depending on how the form is used when a new instance is added to the document it essentially new creates an entire page.

    What I'm trying to hide all instances of subform ("Info"), except the LAST instance that is always on the last page.

    Theoretically, by hiding all previous subform instances (for example, 1-4) and keep only the last instance, it will reduce the number of pages 5-to-1.

    Hope it makes sense.

    SO I guess I need a way to get the number of instances of complete subform, then hide all instances, but the last instance (via a button click).

    Thanks in advance for your help.

    I belive I've thought about it.

  • Cannot print continue on all pages except the last page in the xml editor

    I designed a model that prints continues on all pages except the last page in my xml report. Currently, it works very well when the report has two continuous pages prints the first page and last page prints my footer which is exactly my requirement. But if the release of the report is more that continue to two pages, that prints the first page and last page prints my footer but in the pages of the Middle continue is not printed

    Can someone help me with this problem. Its urgent

    Thank you

    Neednt have you checked.
    If you have inserted a front section break , and then click the new section and remove "link to the previous.

    This will ensure that you will have the same header/foot in all but the last page.

    See the user guide
    http://download.Oracle.com/docs/CD/E10415_01/doc/bi.1013/e12187/T421739T481157.htm#4535357

  • Replace all occurrences of a particular fon.

    I work with a Muse model that uses a font that I can't access. Muse uses a default font instead. It will take forever to replace all instances of the default font with the new font I want to use. Is there a quick way to find a particular font and replace all instances of it?

    Thanks in advance,

    Glen

    Probably the easiest to set up a new paragraph styles, then apply to all the text you want to change.

  • Battery emptied quickly after the last update: 1.0.04.12210

    Hello

    Battery is discharged very quickly after the last update of the firmware (1.0.04.12210). In two days (after a full charge) with about 2 hours of every day, the battery was empty.

    If nobody noticed something similar?

    Is there a solution to this?

    Thanks in advance
    Mr. Genady

    Hello

    Battery is discharged very quickly after the last update of the firmware (1.0.04.12210). In two days (after a full charge) with about 2 hours of every day, the battery was empty.

    If nobody noticed something similar?

    Is there a solution to this?

    Thanks in advance
    Mr. Genady

    In the end, I had to root and re - calibrate the battery as shown on the MobileRead Forum. Now, everything's fine.
    It's too bad that Sony does not provide regular ways to do the same.

  • Strange problem - Timeline plays the empty frames after the last keyframe?

    Again, I thank you all for being so supportive for the beginner!

    So I have a weird problem at hand. Why is that on my calendar (and preview), my animation still plays after my last keyframe?

    In other words, my animation ends to frame 410 and there is nothing after that, but for some reason any, it continues to play until near 695?

    This causes my image to disappear in the frames after 410 when I intended to keep it even after that (with no loop).

    How can I realize what actually remain after image 410? Basically, it's only 3 roses in bloom. I just want that it to bloom when the viewer enters the Web page and stop after that. The last image will make the image stay still (with fully flowered roses). A loop of the bloom would be sticky and very unnecessary.

    Thank you!

    I thought about it.

    It turns out that it was a BUG.

    So I noticed that when I click on one of the folders, I put my layers in, it shows that it is 695 images for a long time. So weird because there is absolutely nothing after 410 chassis when I check the layers in the folder individually.

    So what I did I deleted one of the layers in the file to see if this would remove 695 executives. I just deleted and challenge then remove. And VOILA! He became 410 frames and works fine now. It was certainly a bug I didn't no other editing to fix. Just temporarily removed it and put this back layer by unscrewing the button Delete.

  • Export DNxHD with 8 channels audio multi... Problem! the final file only stereo all mixed with 6 empty strings.

    I export DNxHD with 8 channels audio multi... My problem is that the final file all mixed in the first 2 stereo channels with the channels empty rest 6.

    I'm lost on what could be the problem.

    The main audio channel is 8 channels... NO stereo.
    Export, I chose DNxHD and audio 48000 Hz with 8 output channels


    What could this be? I need to deliver a film with the 8 mono channels.

    Really appreciate all the thoughts/help!

    Thank you

    Christian

    That's how I would implement mixer in CS6's using a sequence of 8 track with 8 mono channels (mono channels using maintains levels matching, stereo and standard tracks can cause differences in level of 3db). Note the Red output channel selection and the panpot position indicated in blue.

  • There is an option to 'save all tabs before closing' - what happened with the last update?

    In formats previous Mozilla, if I had more than 1 tablet open as: Facebook, Aol.comand much much more. When I was ready to close the window and hit the 'x' at the top right, I'd get the options "save tabs & close" "close tabs" "Cancel".

    This gave the option of not having to look for things once again, if you are looking for the same tabs all the time. It's the same exact box you get now, but with the option 'save the tabs and close.

    This option can be added back?

    Thank you for taking the time to listen to...

    Clicking history > restore previous Session, restores your tabs and windows of the previous browsing session in Firefox 4

  • Bought the whole package creative cloud. Everything works fine, all applications are available to install, except the one I was especially interested in what is Photoshop

    After using a trial version of photoshop some time ago he always says that I have to grant the product if I want to use. They push me to log into my account to see if I had a plan for photoshop and say that I don't have, while clearly..., however, I have no idea how to approach the issue, as all other apps seem to work?

    Hi Emil,

    To reset the hosts file using Adobe cleaning tool: -.

    https://helpx.Adobe.com/creative-cloud/KB/CC-cleaner-tool-installation-problems.html

  • Where is the "save all tabs and close" button available in the last window to-be-shut-down past in the latest version?

    It was so VERY PRACTICAL because we didn't need stuff to bookmark that they wish to work the next day... and he disappeared from the latest version of firefox... Or is the option hidden somewhere? thanx

    Now, Firefox always stores the old session which you can access from the history menu by selecting "restore previous Session."

    If you always open the last set of tabs, another approach is the following:

    1. Click on the orange button Firefox, then select options to open the options window
    2. Go to the general Panel
    3. Change the setting "When Firefox starts" to "show my windows and tabs from last time '.
  • Regex does not replace all instances

    Hello
    I have a function which should replace all occurrences of a string in a text box, but it only replaces the first found instance. No idea what I am doing wrong?

    var searchterm:String = stringval.text;
    var model: RegExp = new RegExp (searchterm, "*");
    RTETextArea.text = RTETextArea.text.replace (pattern, replaceval.text);

    I was using the wrong flag: 'g' for global must be used, and 'i' will ignore case.

    var model: RegExp = new RegExp (searchterm, "gi");

  • Re: Replace all NULL values by '0' in the entire table at a time

    Hi all
    I want to replace all null with '0' (zero) values in the columns all at once. Is there a query and the procedure.

    Thanks in advance.

    UPDATE tablename SET column = RequiredValue
    WHERE appropriate_restriction (s)
    Altwernatively you can use NVL:
    Setting a DAY tablename SET column = NVL (RequiredColumn, RequiredValue)

  • replace only the last 3 characters within a string

    Hi all
    I have the string "abcdef_01". How can I replace the last 3 characters with "000"? in this string, for example "_01" should be replaced by '000'. Any way to do it, without the concatenation of the original string with the last 3 characters replaced by "000"?
    Thank you

    Edited by: Roger22 the 18.10.2011 09:29

    Yes, you can...

    SELECT rpad(substr('abcdef_01',1,length('abcdef_01')-3),length('abcdef_01'),chr(dbms_random.value(32,64))
                                                                             || chr(dbms_random.value(32,64))
                                                                             || chr(dbms_random.value(32,64)))
    FROM dual;
    

    RPAD/lpad may use a third parameter that is longer than 1 character and - if the duration of param 1 + length of parameter 3 is longer that parameter 2 - truncates the result to fit the length of parameter 2.

    HTH

Maybe you are looking for

  • USB Mass Storage Device problem

    I have the HP Pavilion Elite E9120f desktop computer. Whenever I start the computer, it shows that I have USB mass storage drives, G, H, I and J. However, there is no attached USB mass storage device. No one knows why this happens? I just usually cli

  • HDR - AZ1 external microphone?

    Is it possible to use an external microphone with the HDR - AZ1, through mini-USB, Wi - fi or an Iphone?

  • Player of the Extender

    Hi everyone, when I start my computer, I get a message saying "invalid or missing resource in the installation directory files. Please reinstall Extender Player"I'm a total novice in all things computer so if anyone knows how to fix this problem as E

  • Software update Q10 Blackberry Q10 newer blackBerry problems

    I just installed the latest version of the software 10.2.1.537 on my Q10.  I had a few problems; 1. my phone is now very slow (I took the battery several times already.). 2. Although my contacts are intact, the headers of my text messages no longer s

  • HP F4580 - disable status monitor

    Is there a way to disable ink level alert/warning that appears whenever I connect?  The ink is low but I do not print a lot so I'm not going to change the cartridge up to what its empty.  The alert is so boring and tedious.