I have this line in a text file which is in the following format:
/ text
/
I need to edit the line and remove text and have a result like this:
/
/
What regex should I use to remove the text? I have a problem because one "/" is in the line below.
How about this?
public String doMagic()
{
return "/\n /";
}
If your trying to remove all characters after a "/" you can do:
String in = "/ text\n /";
String out = in.replaceAll("/.*", "/");
you can use this regexp if line "/" starts and you don't need anything after it:
String in = "/ text\n /";
String pattern = "^(/)(.+?)(\\n.*)";
System.out.println(in.replaceAll(pattern, "$1$3"));
Related
So I have an initial file set:
file1.txt
file2.txt
When I make a change to these files and save them, I append a time stamp to them, so they'd become:
fileN_DD-Mon-YYYY_HHMMSS.txt
But if I was to make any additional saves, the timestamps would begin stacking:
fileN_DD-Mon-YYYY_HHMMSS_DD-Mon-YYYY_HHMMSS.txt
I need a way to get the substring that occurs before the first occurrence of either "." or "_" to get the string that is before them (i.e., actual file name ("fileN")).
I've gotten to this point with
int lastDot = fileName.getName().lastIndexOf('.');
String renamed = fileName.getName().substring(0,lastDot) + getDateTime() + fileName.getName().substring(lastDot);
I've tried using Scanner::useDelimiter to get the first occurrance of a "." or "_" using regexes but no luck.
String renamed = savedFileName(fileName)
public static String savedFileName(String fileName) {
final String TXT = ".txt";
Scanner s = new Scanner(fileName);
s.useDelimiter(<regex>);
String trueFileName = s.next();
s.close();
return trueFileName + getDateTime() + TXT;
for the regex, I've tried "\\W", but that returns just the latest timestamp:
_DD-Mon-YYYY_HHMMSS.txt
, and ".|_" but that returns this monstrosity:
fileN.txt_DD-Mon-YYYY.txt_(more timestamps).txt.
You can use String's split method with regex pattern \.|_:
String longFile = "fileN_DD-Mon-YYYY_HHMMSS.txt";
String shortFile = "file1.txt ";
String pattern = "\\.|_"; // need to escape backslash
System.out.println(longFile.split(pattern)[0]);
System.out.println(shortFile.split(pattern)[0]);
Or, equivalently, regex [._].
Output:
fileN
file1
In the follwing String
String toBeFormatted= "[[LngLatAlt{longitude=-7.125924901999952, latitude=33.831783175000055, altitude=NaN},
LngLatAlt{longitude=-5.401396163999948, latitude=35.92213140900003, altitude=NaN}]]"
1- I need to replace all "LngLatAlt{longitude=" with open bracket "["
2- also need to replace all the intermediate ", latitude=33.831783175000055, altitude=NaN}" with ",33.831783175000055]"
That way my string result :
"[[[-7.125924901999952,33.831783175000055],[-5.401396163999948,35.92213140900003]]]"
try it the following reg exp :
String regexTarget = "(\\[\\[LngLatAlt\\{longitude=)";
toBeFormatted.replaceAll(regexTarget, "\\[\\[\\[");
String regexTarget0 = "(, altitude=NaN\\}, LngLatAlt\\{longitude=)";
toBeFormatted.replaceAll(regexTarget0, "],\\[");
String regexTarget1 = "(, latitude=)";
toBeFormatted.replaceAll(regexTarget1, " ,");
String regexTarget2 = "(, altitude=NaN\\})";
toBeFormatted.replaceAll(regexTarget2, "]");
but it seems not working.
Thank you for your help.
try something like:
String result = toBeFormatted.replaceAll("LngLatAlt\\{longitude=([^,]+), latitude=([^,]+), ([^}]+)\\}", "[$1, $2]");
System.out.println(result);
need some help on pattern mathcing; I need to extract just the filename from a string like:
https://www.testsite.com/files/form/anonymous/api/library/ecb198be-1f05-4b0b-b0cd-7d878488a8c4/document/050cc508-1ea6-4b5f-a22b-b3edbdf6291f/media/x.jpg
just the x.jpg part
& also from this string:
<img alt="/JAGC/Images?action=AttachFile&do=get&target=Images/x.jpg">
& if they are the same image, then replace the target with the URL string.
I can regex out the the
any help please?
It doesn't need any regexp.
Use like this:
String code = "...";
String filename = code.substring(code.lastIndexOf("/")+1, code.length());
Edit:
And in the second case, you dont need the ending of the tag, so use code.length()-2
It's as simple as this:
String filename1 = url.replaceAll(".*/([^/]+)", "$1");
String filename2 = xml.replaceAll(".*/([^\"]+)\".*", "$1");
if (filename1.equals(filename2))
xml = xml.replaceAll("(.*/)([^\"]+)(\".*)", "$1" + url + "$3");
Try this:
str.replaceAll("^.*([a-z]+\\.[a-z]+).*$","$1");
The () group the filename to $1.
Is there a simple solution to parse a String by using regex in Java?
I have to adapt a HTML page. Therefore I have to parse several strings, e.g.:
href="/browse/PJBUGS-911"
=>
href="PJBUGS-911.html"
The pattern of the strings is only different corresponding to the ID (e.g. 911). My first idea looks like this:
String input = "";
String output = input.replaceAll("href=\"/browse/PJBUGS\\-[0-9]*\"", "href=\"PJBUGS-???.html\"");
I want to replace everything except the ID. How can I do this?
Would be nice if someone can help me :)
You can capture substrings that were matched by your pattern, using parentheses. And then you can use the captured things in the replacement with $n where n is the number of the set of parentheses (counting opening parentheses from left to right). For your example:
String output = input.replaceAll("href=\"/browse/PJBUGS-([0-9]*)\"", "href=\"PJBUGS-$1.html\"");
Or if you want:
String output = input.replaceAll("href=\"/browse/(PJBUGS-[0-9]*)\"", "href=\"$1.html\"");
This does not use regexp. But maybe it still solves your problem.
output = "href=\"" + input.substring(input.lastIndexOf("/")) + ".html\"";
This is how I would do it:
public static void main(String[] args)
{
String text = "href=\"/browse/PJBUGS-911\" blahblah href=\"/browse/PJBUGS-111\" " +
"blahblah href=\"/browse/PJBUGS-34234\"";
Pattern ptrn = Pattern.compile("href=\"/browse/(PJBUGS-[0-9]+?)\"");
Matcher mtchr = ptrn.matcher(text);
while(mtchr.find())
{
String match = mtchr.group(0);
String insMatch = mtchr.group(1);
String repl = match.replaceFirst(match, "href=\"" + insMatch + ".html\"");
System.out.println("orig = <" + match + "> repl = <" + repl + ">");
}
}
This just shows the regex and replacements, not the final formatted text, which you can get by using Matcher.replaceAll:
String allRepl = mtchr.replaceAll("href=\"$1.html\"");
If just interested in replacing all, you don't need the loop -- I used it just for debugging/showing how regex does business.
I have a URL and I want it to look like this:
Action Manatee - Action
http://xxxxxx.com/songs2/Music%20Promotion/Stream/Action%20Manatee%20-%20Action.mp3
What is the syntax for trimming up to where it after this "Stream/" and make spaces where the %20 is. I also want to trim the .mp3
Hmm, for that particular example, I would split the string according to the '/' character then trim the text that follows the final '.' character. Finally, do a replace of "%20" into " ". That should leave you with the string you want
Tested
String initial = "http://xxxxxx.com/songs2/Music%20Promotion/Stream/Action%20Manatee%20-%20Action.mp3";
String[] split = initial.split("/");
String output = split[split.length-1];
int length = output.lastIndexOf('.');
output = output.substring(0, length);
output = output.replace("%20", " ");
String urlParts[] = URL.split("\/");
String urlLast = urlParts[length-1];
String nameDotMp = urlLast.replaceAll("%20");
String name = nameDotMp.substring(0,nameDotMp.length-5);
You could use the split() and replace() methods to accomplish this, here are two ways:
Split your string apart by using the forward slashes:
string yourUrl = [URL Listed];
//Breaks your URL into sections on slashes
string[] sections = yourUrl.split("\/");
//Grabs the last section after the slashes, and replaces the %20 with spaces
string newString = sections[sectiongs.length-1].replace("%20"," ");
Split your string at the Stream/ section: (Only use this if you can guarantee it will be in that form)
string yourUrl = [URL Listed];
//This will get everything after Stream (your song name)
string newString = yourUrl.split("Stream\/")[1];
//Replaces your %20s with spaces
newString = newString.replace("%20"," ");
URL songURL = new URL("yourpath/filename");
String filename = songURL.getFile();