0

I have a long text which contains paths to some files.

What I want to do is to remove the paths from it. The filepaths are all something like:

some text 1
/all/extraItems/Java/.Scripts/.Sample1.js
some text 2
/all/extraItems/Android/.Scripts/.Sample2.js
some text 3

I know that using "^/all" will select a sentence that starts with /all and also .js$ for ending with .js. But I cannot merge these together to select the whole filepath.

After all the Regular Expression should be placed on the following code to remove the Paths.

text.replaceAll(<RegularExpression> , "");

Who can I write the regEx for it? Is there any tool?

mammadalius
  • 3,263
  • 6
  • 39
  • 47

2 Answers2

1

use .*? to merge them together

^/all.*?\.js$

So,the regex says match 0 to n number of characters i.e .*? which begins with all and ends with \.js..you need to escape . since . means match any character...

Dont forget to use multiline option

Anirudha
  • 32,393
  • 7
  • 68
  • 89
  • @mammadalius multiline option matches `\n`..more info [here](http://stackoverflow.com/questions/4154239/java-regex-replaceall-multiline) – Anirudha Oct 23 '12 at 18:26
  • i just add another back slash to avoid compiler error "^/all.*?\\.js$" but nothing removed from my text – mammadalius Oct 23 '12 at 21:35
1

If you want to "merge" them you will have to match the characters in between, as well:

^/all.*?\.js$

. matches any character, * repeats the previous character 0 or more times, ? makes the repetition ungreedy, avoiding a match going over multiple lines. Since you are using Java, you need to escape the backslash once more in your string:

"^/all.*?\\.js$"

If you do not use the multiline option already (which you actually need to do, for ^/all and js$ to work on your input), you can do that like this:

"(?m)^/all.*?\\.js$"
Martin Ender
  • 43,427
  • 11
  • 90
  • 130
  • that doesn't help me in fixing it ;)... what happens instead? – Martin Ender Oct 23 '12 at 21:36
  • I even try it on Notepad++ and that select nothing. `^/all` works individually. – mammadalius Oct 23 '12 at 21:51
  • 1
    @mammadalius, works fine for me in Notepad++. Make sure you have the latest version (Notepad++ 6), the previous regex engine had a few issues. To use the regex in Java you need to double the `\` (sorry, I should have mentioned that), and as Fake.It.Til.U.Make.It mentioned, you need to activate multiline mode, but from your two working examples, I thought you had already done that (otherwise `^/all` and `js$` would not work either). I updated my answer, have a look! – Martin Ender Oct 24 '12 at 08:23