I have a string of SVG markup that contains multiples of these:
url(#586-xr___83_193_101__rgba_243_156_18_1__0-rgba_243_156_18_1__100)
and I need them to be like this:
url('#586-xr___83_193_101__rgba_243_156_18_1__0-rgba_243_156_18_1__100')
with quotes inside the parenthesis.
These will be mixed inside a long string containing lots of different markup, so needs to be very accurate.
You can use a regex like this:
\((.*?)\)
With the replacement string ('$1')
The idea is capture everything within parentheses and concatenates the '
So, you can use a code like this:
String str = "url(#586-xr___83_193_101__rgba_243_156_18_1__0-rgba_243_156_18_1__100)";
str = str.replaceAll("\\((.*?)\\)", "('$1')");
//Outuput: url('#586-xr___83_193_101__rgba_243_156_18_1__0-rgba_243_156_18_1__100')
IdeOne example
In case you want a better performance regex you can use:
str = str.replaceAll("\\(([^)]*)\\)", "('$1')");
ReplaceAll remove a part of the string and put an unrelated and invariant new stuff instead.
Because the replacement string can't be the same at both side, the only solution I imagine (with the constraint of using RegEx and ReplaceAll) is to do it in two time:
String Str = "url(#586-xr___83_193_101__rgba_243_156_18_1__0-rgba_243_156_18_1__100)";
Str = Str.replaceAll("\\(", "('"); // replace left parenthesis
Str = Str.replaceAll("\\)", "')"); // replace right parenthesis
System.out.print("Return Value: " + Str);
// Return Value: url('#586-xr___83_193_101__rgba_243_156_18_1__0-rgba_243_156_18_1__100')
You can test it here.
Related
I have a String as shown below:
String s = "A, Category, \"Agriculture, forestry and fishing\",";
I want to remove spaces around comma (which are outside quotes). So my string should look like:
A,Category,"Agriculture, forestry and fishing",
I tried following RE:
String s = s.replaceAll("[,]\\s+", ",");
but output is:
A,Category,"Agriculture,forestry and fishing",
What changes should I do in my regular expression to avoid changes for commas inside quotes?
You can use this :
String str = "A, Category, \"Agriculture, forestry and fishing\",";
String result = str.replaceAll(" (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", "");
//------------------------------^--------------------------------------
System.out.println(result);
This will print :
A,Category,"Agriculture, forestry and fishing",
//----------------------^---------------------
The space inside the quotes is not changes, just outside the quotes.
Here is a code DEMO and here is a regex DEMO
EDIT
This part is from #Exception_al so he suggest to use this :
String result = str.replaceAll("(\\s*,\\s*)(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", ",");
This solution is very good if you want to replace all spaces between comma , and the word.
You can check how this can work in regex DEMO
I need to replace some of the contents of a large string with different content in the same spot.
Lets say I have a string:
String str = "package blah.foo.bar;\n"
+ "import org.blah.blah;\n"
+ "import sel.this.that;\n"
+ "import sel.boo.foo;\n"
+ "...more text";
I want to insert the word gen into statements which start with import sel. So that the end result looks like: import sel.gen.this.that;
I have had success with the following method but only when I KNOW where that substring will be within the string. My issue is that I'm not sure how to make the following code dynamic:
private String modifyString(String str){
String hold = str.substring(0, str.indexOf(";"));
hold = new StringBuilder(hold).insert(13, "gen.").toString();
str = str.replace("(?m)^package.*", hold);
return str;
}
The above code correctly replaces the strings package blah.foo.bar; with package blah.gen.foo.bar;
But again this only works with replacing the beginning portion of the string. I'm looking to be able to hold all instances of a substring and then insert a substring within those substrings.
You can use a simple line like:
str = str.replaceAll("import sel\\.", "import sel.gen.");
Regex demo
IdeOne example
replace takes a literal string expression as an argument, not a regex.
This should work and is simpler than using a regex:
String result = str.replace("import sel.", "import sel.gen.");
I have string like this String s="ram123",d="ram varma656887"
I want string like ram and ram varma so how to seperate string from combined string
I am trying using regex but it is not working
PersonName.setText(cursor.getString(cursor.getColumnIndex(cursor
.getColumnName(1))).replaceAll("[^0-9]+"));
The correct RegEx for selecting all numbers would be just [0-9], you can skip the +, since you use replaceAll.
However, your usage of replaceAll is wrong, it's defined as follows: replaceAll(String regex, String replacement). The correct code in your example would be: replaceAll("[0-9]", "").
You can use the following regex: \d for representing numbers. In the regex that you use, you have a ^ which will check for any characters other than the charset 0-9
String s="ram123";
System.out.println(s);
/* You don't need the + because you are using the replaceAll method */
s = s.replaceAll("\\d", ""); // or you can also use [0-9]
System.out.println(s);
To remove the numbers, following code will do the trick.
stringname.replaceAll("[0-9]","");
Please do as follows
String name = "ram varma656887";
name = name.replaceAll("[0-9]","");
System.out.println(name);//ram varma
alternatively you can do as
String name = "ram varma656887";
name = name.replaceAll("\\d","");
System.out.println(name);//ram varma
also something like given will work for you
String given = "ram varma656887";
String[] arr = given.split("\\d");
String data = new String();
for(String x : arr){
data = data+x;
}
System.out.println(data);//ram varma
i think you missed the second argument of replace all. You need to put a empty string as argument 2 instead of actually leaving it empty.
try
replaceAll(<your regexp>,"")
you can use Java - String replaceAll() Method.
This method replaces each substring of this string that matches the given regular expression with the given replacement.
Here is the syntax of this method:
public String replaceAll(String regex, String replacement)
Here is the detail of parameters:
regex -- the regular expression to which this string is to be matched.
replacement -- the string which would replace found expression.
Return Value:
This method returns the resulting String.
for your question use this
String s = "ram123", d = "ram varma656887";
System.out.println("s" + s.replaceAll("[0-9]", ""));
System.out.println("d" + d.replaceAll("[0-9]", ""));
i have a link http://localhost:8080/reporting/pvsUsageAction.do?form_action=inline_audit_view&days=7&projectStatus=scheduled&justificationId=5&justificationName= No Technicians in Area in my struts based web application.
The variable in URL justificationName have some spaces before its vales as shown. when i get value of justificationName using request.getParameter("justificationName") it gives me that value with spaces as given in the URL. i want to remove those spaces. i tried trim() i tries str = str.replace(" ", ""); but any of them did not removed those spaces. can any one tell some other way to remove the space.
Noted one more thing that i did right click on the link and opened the link into new tab there i noticed that link looks like.
http://localhost:8080/reporting/pvsUsageAction.do?form_action=inline_audit_view&days=7&projectStatus=scheduled&justificationId=5&justificationName=%A0%A0%A0%A0%A0%A0%A0%A0No%20Technicians%20in%20Area
Notable point is that in the address bar it shows %A0 for white spaces and also show %20 for space as well see the link and tell the difference please if any one have idea about it.
EDIT
Here is my code
String justificationCode = "";
if (request.getParameter("justificationName") != null) {
justificationCode = request.getParameter("justificationName");
}
justificationCode = justificationCode.replace(" ", "");
Note: replace function remove the space from inside the string but not removing starting spaces.
e-g if my string is " This is string" after using replace it becomes " Thisisstring"
Thanks in advance
Strings are immutable in Java, so the method doesn't change the string you pass but returns a new one. You must use the returned value :
str = str.replace(" ", "");
Manual trim
You need to remove the spaces the string. This will remove any number of consecutive spaces.
String trimmed = str.replaceAll(" +", "");
If you want to replace all whitespace characters:
String trimmed = str.replaceAll("\\s+", "");
URL Encoding
You could also use an URLEncoder, which sounds like a more appropriate way to go:
import java.net.UrlEncoder;
String url = "http://localhost:8080/reporting/" + URLEncoder.encode("pvsUsageAction.do?form_action=inline_audit_view&days=7&projectStatus=scheduled&justificationId=5&justificationName= No Technicians in Area", "ISO-8859-1");
You have to assign the result of the replace(String regex, String replacement) operation to another variable. See the Javadoc for the replace(String regex, String replacement) method. It returns a brand new String object and this is because the String(s) in Java are immutable. In your case, you can simply do the following
String noSpacesString = str.replace("\\s+", "");
You can use replaceAll("\\s","") It will remove all white space.
If you are trying to remove the trailing and ending white spaces, then
s = s.trim();
Or if you want to remove all the spaces the use :
s = s.replace(" ","");
There are two ways of doing one is regular expression based or your own way of implementing the logic
replaceAll("\\s","")
or
if (text.contains(" ") || text.contains("\t") || text.contains("\r")
|| text.contains("\n"))
{
//code goes here
}
Suppose I would like to remove all " surrounding a string. In Python, I would:
>>> s='"Don\'t need the quotes"'
>>> print s
"Don't need the quotes"
>>> print s.strip('"')
Don't need the quotes
And if I want to remove multiple characters, e.g. " and parentheses:
>> s='"(Don\'t need quotes and parens)"'
>>> print s
"(Don't need quotes and parens)"
>>> print s.strip('"()')
Don't need quotes and parens
What's the elegant way to strip a string in Java?
Suppose I would like to remove all " surrounding a string
The closest equivalent to the Python code is:
s = s.replaceAll("^\"+", "").replaceAll("\"+$", "");
And if I want to remove multiple characters, e.g. " and parentheses:
s = s.replaceAll("^[\"()]+", "").replaceAll("[\"()]+$", "");
If you can use Apache Commons Lang, there's StringUtils.strip().
The Guava library has a handy utility for it. The library contains CharMatcher.trimFrom(), which does what you want. You just need to create a CharMatcher which matches the characters you want to remove.
Code:
CharMatcher matcher = CharMatcher.is('"');
System.out.println(matcher.trimFrom(s));
CharMatcher matcher2 = CharMatcher.anyOf("\"()");
System.out.println(matcher2.trimFrom(s));
Internally, this does not create any new String, but just calls s.subSequence(). As it also doesn't need Regexps, I guess its the fastest solution (and surely the cleanest and easiest to understand).
In java, you can do it like :
s = s.replaceAll("\"",""),replaceAll("'","")
Also if you only want to replace "Start" and "End" quotes, you can do something like :
s = s.replace("^'", "").replace("'$", "").replace("^\"", "").replace("\"$", "");
OR if simply put :
s = s.replaceAll("^\"|\"$", "").replaceAll("^'|'$", "");
This replaces " and () at the beginning and end of a string
String str = "\"te\"st\"";
str = str.replaceAll("^[\"\\(]+|[\"\\)]+$", "");
try this:
new String newS = s.replaceAll("\"", "");
replace the double-quote with a no-character String.