I have an String
String string = "-minY:50 -maxY:100 -minVein:8 -maxVein:10 -meta:0 perChunk:5;";
And I want to somehow get the -meta:0 out of it with regex (replace everything except -meta:0), I made an regex which deletes -meta:0 but I can't make it delete everything except -meta:0
I tried using some other regex but it was ignoring whole line when I had -meta:[0-9] in it, and like you can see I have one line for everything.
This is how it has been deleting -meta:0 from the String:
String meta = string.replaceAll("( -meta:[0-9])", "");
System.out.println(meta);
I just somehow want to reverse that and delete everything except -meta:[0-9]
I couldn't find anything on the page about my issue because everything was ignoring whole line after it found the word, so sorry if there's something similar to this.
You should be capturing your match in a captured group and use it's reference in replacement as:
String meta = string.replaceAll("^.*(-meta:\\d+).*$", "$1");
System.out.println(meta);
//=> "-meta:0"
RegEx Demo
As I understand your requirement you want to :
a) you want to extract meta* from the string
b) replace everything else with ""
You could do something like :
String string = "-minY:50 -maxY:100 -minVein:8 -maxVein:10 -meta:0 perChunk:5;";
Pattern p = Pattern.compile(".*(-meta:[0-9]).*");
Matcher m = p.matcher(string);
if ( m.find() )
{
string = string.replaceAll(m.group(0),m.group(1));
System.out.println("After removal of meta* : " + string);
}
What this code does is it finds meta:[0-9] and retains it and removes other found groups
Related
I have strings that contain URL encoding (%22) and other characters [!##$%^&*]. I need to use a RegEx to check if the string contains a character within that group but exclude the URL encoded quote (%22). I can't get the negative look ahead to work properly nor am I able to get an excluded string (or negation) working either. Can someone help? Here is code so far that doesn't work:
Pattern p = Pattern.compile("[!##$%^&*]"); //
String[] tokens = {"%22Hobo%22", "Shoe*", "Rail%6Road","Sugar"};
for (String string : tokens) {
Matcher m = p.matcher(string);
boolean b = m.find()
System.out.println(string + ": " + b);
}
The desired output should be false, true, true, false.
(?!%22)[!##$%^&*]
Try this.See demo.
https://regex101.com/r/mS3tQ7/16
export const uriParser = (x) =>
//replace/regex exclude-negated [set-of-tokens], doesn't work/parse for (%[A-Fa-f0-9]{2})+
//decodeURI() does the same I believe, but this will always return a string,
//without an error object
//a-z or A-Z includes underscore '_' but not space whitespace, nor (\r\n|\r|\n)+
x.replace(/(%[A-Fa-f0-9]{2})+[^a-zA-Z0-9-+ ]+/g, "_");
https://www.ietf.org/rfc/rfc3986.txt#:~:text=2.4.%20%20When%20to%20Encode%20or%20Decode%0A
for my purposes I make my uri link fragmens go thru (%[A-Fa-f0-9]{2})+ on mount so I use .replace("_"," ") for ui but uriParser() for outgoing links in ux to bypass redundancy as much as possible. The use case choice is between getting a string always & putting specifications for other characters before this. "Whyo you do not use a URLEncoder?" – Jens' comment on question
I have a problem - I can't seem to be able to remove the new lines/spaces from the beginning/end of a string. I use \s in the beginning and end of the regex and even use .trim() after I get the string, but to no avail.
public void extractInfo(String mydata) {
// regex to extract the user name
Pattern pattern = Pattern.compile("user:\\s*(.*)\\s+branch");
Matcher matcher = pattern.matcher(mydata);
// regex to extract the branch name
Pattern pattern2 = Pattern.compile("branch:\\s*(.*)\\s+changed");
Matcher matcher2 = pattern2.matcher(mydata);
// regex to extract the comment and write it in a variable
comment = mydata.replaceAll("(?s)\\s.*java;[0-9,.]+|.*java;NONE\\s", "");
// put the author name in a variable
matcher.find();
author = matcher.group(1).toString();
// put the branch name in a variable
matcher2.find();
branch = matcher2.group(1).toString();
author.trim();
comment.trim();
branch.trim();
}
This is what I use to extract the info.
This is the output I get (lines kept), after I append the extracted information using StringBuilder:
git log --all -100 --before="2013-03-11" --branches=HEAD
--author="\(cholakov\)" --grep="^[#]*[0]*23922:[ ]*user:
Fixed the message for defaulted bonds " --pretty="%H - %s ; %ad"
The new line after user: is what causes the whole command to fail when I try to execute it in cmd, that's what I need fixed.
And this is my input (can't seem to be able to keep the formatting, DataObjectParser.java;1.94 is on a new line and there is no line skipped between each line):
user: cholakov
branch: HEAD
changed files:
DataObjectParser.java;1.94
Fixed the message for defaulted bonds
author.trim();
is a no-op since String is an immutable class. Use
author = author.trim();
calling author.trim RETURNS a new String, but it does not replace the one you call it from.
The trim function returns a copy of the string, with leading and trailing whitespace omitted. You should do this instead:
author = author.trim();
comment = comment.trim();
branch = branch.trim();
I think that you can completely remove the .trim() in the end if you change your regex a bit:
Pattern pattern = Pattern.compile("user:\\s*(.*?)\\s+branch");
Pattern pattern2 = Pattern.compile("branch:\\s*(.*?)\\s+changed");
comment = mydata.replaceAll("(?s)\\s*.*java;(?:[0-9,.]+|NONE)\\s*", "");
I tweaked your regex a little in each; namely made some (.*) into (.*?) so that you can remove all the trailing spaces and simplified your replace comment a bit. Try to see if that solves your issues ^^
EDIT:
Try running one last replace on the comment:
comment = comment.replaceAll("^\\s*|\\s*$", "");
Hey Try to run this stuff :
public class ReadFile {
public static void main(String[] args) {
String line = "\n Java ";
System.out.println(line.trim());
}
}
I have a string that contains a "table name" that I would like to extract out of this string. Basically from this string below, I would like to just grab "test_table". The String always designates "Table name=", but I am having trouble with walking this string and pulling out the table name that I need.
I need to grab each char until I hit the comma, but I am having trouble. An example string looks like this:
{newModel=Table name=test_table, nameInSource=null, uuid=tid:f1f46c57b618-b9a0d09f-00000001}model
Thanks in advance.
Use a regular expression with a matching group around the part you want. The example below looks for the substring Table name= in the target string then captures every character until it finds a comma in the group numbered one. Finally, if the pattern was found then it returns the characters in group one or "null".
public static String parseTableName(String s) {
Pattern p = Pattern.compile("Table name=([^,]*)");
Matcher m = p.matcher(s);
return m.find() ? m.group(1) : null;
}
// ...
parseTableName(yourString); // => "test_table"
What have you tried? There are a couple of approaches:
You could use a regex to match the key=value pattern to pull it out that way.
You could also just use JSON which is more standard to parse (plenty of libraries to do that).
You could strip out the characters { , } and do a string split on =
In your sample it actually looks like the key your looking for is "name" not "Table name" (where "Table" is the value of the previous "newModel" key. In any case, there are many ways to do this in Java. Assuming you don't know the order of keys/values in the string, I would use the StringTokenizer to split it up by commas, and then cycle through each key to see which is "name", and then use
String tableName;
StringTokenizer st = new StringTokenizer(in, ",");
while(st.hasMoreTokens()) {
String myString= st.nextToken();
if (myString.startsWith("name") {
tableName=myString.substr(myString.charAt("="), myString.length()+2);
}
break;
}
I want to remove any substring(s) in a string that begins with 'galery' and ends with 'jssdk));'
For instance, consider the following string:
Galery something something.... jssdk));
I need an algorithm that removes 'something something....' and returns 'Galery jssdk));'
This is what I've done, but it does not work.
newsValues[1].replaceAll("Galery.*?jssdK));", "");
Could probably be improved, I've done it fast:
public static String replaceMatching(String input, String lowerBound, String upperBound{
Pattern p = Pattern.compile(".*?"+lowerBound+"(.*?)"+upperBound+".*?");
Matcher m = p.matcher(input);
String textToRemove = "";
while(m.find()){
textToRemove = m.group(1);
}
return input.replace(textToRemove, "");
}
UPDATE Thx for accepting the answer, but here is a smaller reviewed version:
public static String replaceMatching2(String input, String lowerBound, String upperBound){
String result = input.replaceAll("(.*?"+lowerBound + ")" + "(.*?)" + "(" + upperBound + ".*)", "$1$3");
return result;
}
The idea is pretty simple actually, split the String into 3 groups, and replace those 3 groups with the first and third, droping the second one.
You are almost there, but that will remove the entire string. If you want to remove anything between Galery and jssdK));, you will have to do something like so:
String newStr = newsValues[1].replaceAll("(Galery)(.*?)(jssdK\\)\\);)","$1$3");
This will put the strings into groups and will then use these groups to replace the entire string. Note that in regex syntax, the ) is a special character so it needs to be escaped.
String str = "GaleryABCDEFGjssdK));";
String newStr = str.replaceAll("(Galery)(.*?)(jssdK\\)\\);)","$1$3");
System.out.println(newStr);
This yields: GaleryjssdK));
I know that the solution presented by #amit is simpler, however, I thought it would be a good idea to show you a useful way in which you can use the replaceAll method.
Simplest solution will be to replace the string with just the "edges", effectively "removing" 1 everything between them.
newsValues[1].replaceAll("Galery.*?jssdK));", "GaleryjssdK));");
1: I used "" here because it is not exactly replacing - remember strings are immutable, so it is creating a new object, without the "removed" part.
newsValues[1] = newsValues[1].substring(0,6)+newsValues.substring(newsValues[1].length()-5,newsValues[1].length())
This basically concatenates the "Galery" and the "jssdk" leaving or ignoring everything else. More importantantly, you can simply assign newValues[1] = "Galeryjssdk"
For example I have such a string, in which I must find and replace multiple substrings, all of which start with #, contains 6 symbols, end with ' and should not contain ) ... what do you think would be the best way of achieving that?
Thanks!
Edit:
just one more thing I forgot, to make the replacement, I need that substring, i.e. it gets replaces by a string generated from the substring being replaced.
yourNewText=yourOldText.replaceAll("#[^)]{6}'", "");
Or programmatically:
Matcher matcher = Pattern.compile("#[^)]{6}'").matcher(yourOldText);
StringBuffer sb = new StringBuffer();
while(matcher.find()){
matcher.appendReplacement(sb,
// implement your custom logic here, matcher.group() is the found String
someReplacement(matcher.group());
}
matcher.appendTail(sb);
String yourNewString = sb. toString();
Assuming you just know the substrings are formatted like you explained above, but not exactly which 6 characters, try the following:
String result = input.replaceAll("#[^\\)]{6}'", "replacement"); //pattern to replace is #+6 characters not being ) + '
You must use replaceAll with the right regular expression:
myString.replaceAll("#[^)]{6}'", "something")
If you need to replace with an extract of the matched string, use a a match group, like this :
myString.replaceAll("#([^)]{6})'", "blah $1 blah")
the $1 in the second String matches the first parenthesed expression in the first String.
this might not be the best way to do it but...
youstring = youstring.replace("#something'", "new stringx");
youstring = youstring.replace("#something2'", "new stringy");
youstring = youstring.replace("#something3'", "new stringz");
//edited after reading comments, thanks