Help with strREReplaceAll + numbers

Hello,

I'm new to java programming and have been thrown into some code at work. I have a file to work with.

The file name used to be just filename.111, but was recently changed to 2013191filename.111. We have a java script that drops the word filename and just leave the ".111". Now I need to drop the "2013191filename". Only problem is, the 2013191 changes, so tomorrow the filename will be 2013192, then 2013193 ect ect....

This is the existing string:

string str_new_FileName = strREReplaceAll(str_FileName, "filename.", "" );

I've tried this:

string str_new_FileName = strREReplaceAll(str_FileName, "20[0-9]\{1,3\}filename.", "" );

But you professionals probably already seen that doesn't work. How do I get this to work?

---------- Post updated at 09:10 PM ---------- Previous update was at 08:19 PM ----------

So in short what I want to do is remove the numbers + filename.

Does this make sense?

strREReplaceAll(str_FileName, "[^0-9filename.]", "" );

---------- Post updated at 10:01 PM ---------- Previous update was at 09:10 PM ----------

Or this one?

strREReplaceAll(str_FileName, "[0-9filename.]", "" );

If I understand what you're trying to do correctly, try:

strREReplace(str_FileName, ".*[.]", "" );

Note strREReplace , not strREReplaceAll and note that I haven't tested this and it has been a while since I've written any Java code.

Basically, all I'm looking to do is remove everything up to and including the "." in the filename.

Sample filename = 2013123filename.123
Desired output = 123

---------- Post updated at 11:20 PM ---------- Previous update was at 11:15 PM ----------

Yup, I think that did it. Very cool, thanks!

And what did my suggestion do?

The regular expression .*[.] matches any string of characters up to and including a period. Replacing what that RE matches with an empty string should just leave you with the characters following the period in your input string.

Yes, it worked perfectly. Very cool. Looks like I just learned my first regular expression in Java Code. :smiley:

Thanks again!

REs aren't limited to or "special" in Java. This is a Basic Regular Expression that will work in any utility that processes regular expressions including, but not limited to: awk, ed, ex, grep, java, sed, vi, ...

Learn how to write regular expressions and they will help you with a lot of programming tasks. :slight_smile:

I have another issue where this may help as well. Before this correction, a bunch of files got processed and renamed incorrectly. So now, I have to go back and correct the filenames (about 400 of them).

But I'll post this question in the Shell Scripting section.

Thanks again.