I want to replace a word starting with # in a string which contains set of words with the same word (# omitted)
example
"word1 word2 #user" should be replaced with "word1 word2 user"
Can someone help me?
You can use regex. Lets start with
yourText = yourText.replaceAll("#(\\S+)", "$1");
in regex:
\S represents any non-whitespace characters
+ represents one or more
\S+ represents one or more non-whitespace characters
(\S+) -parenthesis create group containing one or more non-whitespace characters, this group will be indexed as 1
in replacement
$1 in replacement allows us to use content of group 1.
In other words it will try to find #non-whitespaces (which and replace it with non-whitespaces part.
But this solution doesn't require # to be start of word. To do this we could check if before # there is
whitespace space \s,
or start of the string ^.
To test if something is before our element without actually including it in our match we can use look-behind (?<=...).
So our final solution can look like
yourText = yourText.replaceAll("(?<=^|\\s)#(\\S+)", "$1");
yes, String.replaceAll()
String foo = "#user"
foo = foo.replaceAll("#", "");
You have not very clear use case, but my assumptions with code example:
omit all symbols with replaceAll function
omit just first symbol with substring function
public class TestRegex {
public static void main(String[] args) {
String omitInStart = "#user";
String omitInMiddle = "#user";
String omitInEnd = "#user";
String omitFewSymbols = "#us#er";
List<String> listForOmit = Arrays.asList(omitInStart, omitInMiddle, omitInEnd, omitFewSymbols);
listForOmit.forEach(e -> System.out.println(omitWithReplace(e)));
listForOmit.forEach(e -> System.out.println(omitFirstSymbol(e)));
}
private static String omitFirstSymbol(String stringForOmit) {
return stringForOmit.substring(1);
}
private static String omitWithReplace(String stringForOmit) {
String symbolForOmit = "#";
return stringForOmit.replaceAll(symbolForOmit, "");
}
}
Related
I'm trying to replace all characters between two delimiters with another character using regex. The replacement should have the same length as the removed string.
String string1 = "any prefix [tag=foo]bar[/tag] any suffix";
String string2 = "any prefix [tag=foo]longerbar[/tag] any suffix";
String output1 = string1.replaceAll(???, "*");
String output2 = string2.replaceAll(???, "*");
The expected outputs would be:
output1: "any prefix [tag=foo]***[/tag] any suffix"
output2: "any prefix [tag=foo]*********[/tag] any suffix"
I've tried "\\\\\[tag=.\*?](.\*?)\\\\[/tag]" but this replaces the whole sequence with a single "\*".
I think that "(.\*?)" is the problem here because it captures everything at once.
How would I write something that replaces every character separately?
you can use the regex
\w(?=\w*?\[)
which would match all characters before a "[\"
see the regex demo, online compiler demo
You can capture the chars inside, one by one and replace them by * :
public static String replaceByStar(String str) {
String pattern = "(.*\\[tag=.*\\].*)\\w(.*\\[\\/tag\\].*)";
while (str.matches(pattern)) {
str = str.replaceAll(pattern, "$1*$2");
}
return str;
}
Use like this it will print your tx2 expected outputs :
public static void main(String[] args) {
System.out.println(replaceByStar("any prefix [tag=foo]bar[/tag] any suffix"));
System.out.println(replaceByStar("any prefix [tag=foo]loooongerbar[/tag] any suffix"));
}
So the pattern "(.*\\[tag=.*\\].*)\\w(.*\\[\\/tag\\].*)" :
(.*\\[tag=.*\\].*) capture the beginning, with eventually some char in the middle
\\w is for the char you want to replace
(.*\\[\\/tag\\].*) capture the end, with eventually some char in the middle
The substitution $1*$2:
The pattern is (text$1)oneChar(text$2) and it will replace by (text$1)*(text$2)
I have this regex expression:
String patt = "(\\w+?)(:|<|>)(\\w+?),";
Pattern pattern = Pattern.compile(patt);
Matcher matcher = pattern.matcher(search + ",");
I am able to match a string like
search = "firstName:Giorgio"
But I'm not able to match string like
search = "email:giorgio.rossi#libero.it"
or
search = "dataregistrazione:27/10/2016"
How I should modify the regex expression in order to match these strings?
You may use
String pat = "(\\w+)[:<>]([^,]+)"; // Add a , at the end if it is necessary
See the regex demo
Details:
(\w+) - Group 1 capturing 1 or more word chars
[:<>] - one of the chars inside the character class, :, <, or >
([^,]+) - Group 2 capturing 1 or more chars other than , (in the demo, I added \n as the demo input text contains newlines).
You can use regex like this:
public static void main(String[] args) {
String[] arr = new String[]{"firstName:Giorgio", "email:giorgio.rossi#libero.it", "dataregistrazione:27/10/2016"};
String pattern = "(\\w+[:|<|>]\\w+)|(\\w+:\\w+\\.\\w+#\\w+\\.\\w+)|(\\w+:\\d{1,2}/\\d{1,2}/\\d{4})";
for(String str : arr){
if(str.matches(pattern))
System.out.println(str);
}
}
output is:
firstName:Giorgio
email:giorgio.rossi#libero.it
dataregistrazione:27/10/2016
But you have to remember that this regex will work only for your format of data. To make up the universal regex you should use RFC documents and articles (i.e here) about email format. Also this question can be useful.
Hope it helps.
The Character class \w matches [A-Za-z0-9_]. So kindly change the regex as (\\w+?)(:|<|>)(.*), to match any character from : to ,.
Or mention all characters that you can expect i.e. (\\w+?)(:|<|>)[#.\\w\\/]*, .
I wrote a regex which should check does string contains word 'Page' and after it any number
This is code:
public static void main(String[] args) {
String str1 = "12/15/14 7:01:44 Page 10 ";
String str2 = "12/15/14 7:01:44 Page 9 ";
System.out.println(containsPage(str2));
}
private static boolean containsPage(String str) {
String regExp = "^.*Page[ ]{1,}[0-9].$";
return Pattern.matches(regExp, str);
}
Result: str1: false, str2:true
Can you help me what is wrong?
Change the regex to the following:
String regExp = "^.*Page[ ]{1,}[0-9]+.$";
so that it matches one or more digits (hence the [0-9]+).
You also don't need the boundary matchers (^ and $) since Pattern#matches would match the entire input string; and [ ]{1,} is equivalent to [ ]+:
String regExp = ".*Page +[0-9]+.";
Change it to:
String regExp = "^.*Page[ ]{1,}[0-9]+.$"; //or \\d+
↑
[0-9] matches 9 in the second example, and . matches the space.
In the first example, [0-9] matches 1, . matches 0 and remained space isn't matched. Note that ^ and $ are not really needed here.
Your regex can be simplified to:
String regExp = ".*Page\\s+\\d+.";
Hello I've been trying to make some replacement with not success
public class demo {
public static void main(String[] args){
String url = "/demoapi/api/user/123";
String newurl = "/user/?user=$1";
Pattern pattern = Pattern.compile("/^\\/demoapi\\/api\\/user\\/([0-9]\\d*)$/i");
Matcher match = pattern.matcher(url);
}
}
I want to replace $1 with 123 , how do I do this ?!
Thank you !
I want to replace $1 with 123 , how do I do this ?!
Simply use replace method but never forget to escape $
"/user/?user=$1".replace(/(\$1)/,"123");
I think you are looking for something like this:
String url = "/demoapi/api/user/123";
String newurl = "/user/?user=$1";
Pattern pattern = Pattern.compile(".*/user/(\\d*)");
Matcher match = pattern.matcher(url);
if(match.matches()){
newurl = newurl.replace("$1", match.group(1));
}
System.out.println(newurl);
Hope this helps.
You don't need to enter the whole text ^\\/demoapi\\/api\\/user\\ in the pattern. Just a ^.*\\/ will match upto the last / symbol. So Your java code would be,
String url = "/demoapi/api/user/123";
String newurl = "/user/?user=$1";
String m1 = url.replaceAll("(?i)^.*\\/([0-9]+)$", "$1");
String m2 = newurl.replaceAll("\\$1", m1);
System.out.println(m2);
Output:
/user/?user=123
Explanation:
(?i) Turn on the case insensitive mode.
^.*\\/ Matches upto the last / symbol.
([0-9]+)$ Captures the last digits.
IDEONE
OR
String url = "/demoapi/api/user/123";
String m1 = url.replaceAll(".*/(\\d*)$", "/user/?user=$1");
You need to put / before (\\d*), so that it would capture the numbers from starting ie, 123. Otherwise it would print the last number ie, 3.
You can use any of the following method :-
public class Test {
public static void main(String[] args) {
public static void main(String[] args) {
String url = "/demoapi/api/user/123";
String newurl = "/user/?user=$1";
String s1 = newurl.replaceAll("\\$1", Matcher.quoteReplacement("123"));
System.out.println("s1 : " + s1);
// OR
String s2 = newurl.replaceAll(Pattern.quote("$1"),Matcher.quoteReplacement("123"));
System.out.println("s2 : " + s2);
// OR
String s3 = newurl.replaceAll("\\$1", "123");
System.out.println("s3 : " + s3);
// OR
String s4 = newurl.replace("$1", "123");
System.out.println("s4 : " + s4);
}
}
Explanation of Methods Used :
Pattern.quote(String s) : Returns a literal pattern String for the
specified String. This method produces a String that can be used to
create a Pattern that would match the string s as if it were a
literal pattern. Metacharacters or escape sequences in the input
sequence will be given no special meaning.
Matcher.quoteReplacement(String s) : Returns a literal replacement
String for the specified String. This method produces a String that
will work as a literal replacement s in the appendReplacement method
of the Matcher class. The String produced will match the sequence of
characters in s treated as a literal sequence. Slashes ('\') and
dollar signs ('$') will be given no special meaning.
String.replaceAll(String regex, String replacement) : Replaces each
substring of this string that matches the given regular expression
with the given replacement.
An invocation of this method of the form str.replaceAll(regex, repl)
yields exactly the same result as the expression
Pattern.compile(regex).matcher(str).replaceAll(repl)
Note that backslashes () and dollar signs ($) in the replacement
string may cause the results to be different than if it were being
treated as a literal replacement string; see Matcher.replaceAll. Use
Matcher.quoteReplacement(java.lang.String) to suppress the special
meaning of these characters, if desired.
String.replace(CharSequence target, CharSequence replacement) :
Replaces each substring of this string that matches the literal
target sequence with the specified literal replacement sequence. The
replacement proceeds from the beginning of the string to the end, for
example, replacing "aa" with "b" in the string "aaa" will result in
"ba" rather than "ab".
Compact Search: .*?(\d+)$
This is all you need:
String replaced = yourString.replaceAll(".*?(\\d+)$", "/user/?user=$1");
In the regex demo, see the substitutions at the bottom.
Explanation
(\d+) matches one or more digits (this is capture Group 1)
The $ anchor asserts that we are at the end of the string
We replace with /user/?user= and Group 1, $1
In Java I want to insert a space after a String but only if the character after the comma is succeeded by a digit or letter. I am hoping to use the replaceAll method which uses regular expressions as a parameter. So far I have the following:
String s1="428.0,chf";
s1 = s1.replaceAll(",(\\d|\\w)",", ");
This code does successfully distinguish between the String above and one where there is already a space after the comma. My problem is that I can't figure out how to write the expression so that the space is inserted. The code above will replace the c in the String shown above with a space. This is not what I want.
s1 should look like this after executing the replaceAll: "428.0 chf"
s1.replaceAll(",(?=[\da-zA-Z])"," ");
(?=[\da-zA-Z]) is a positive lookahead which would look for a digit or a word after ,.This lookahead would not be replaced since it is never included in the result.It's just a check
NOTE
\w includes digit,alphabets and a _.So no need of \d.
A better way to represent it would be [\da-zA-Z] instead of \w since \w also includes _ which you do not need 2 match
Try this, and note that $1 refers to your matched grouping:
s1.replaceAll(",(\\d|\\w)"," $1");
Note that String.replaceAll() works in the same way as a Matcher.replaceAll(). From the doc:
The replacement string may contain references to captured subsequences
String s1="428.0,chf";
s1 = s1.replaceAll(",([^_]\\w)"," $1"); //Match alphanumeric except '_' after ','
System.out.println(s1);
Output: -
428.0 chf
Since \w matches digits, words, and an underscore, So, [^_] negates the underscore from \w..
$1 represents the captured group.. You captured c after , here, so replace c with _$1 -> _c.. "_" represent a space..
Try this....
public class Tes {
public static void main(String[] args){
String s1="428.0,chf";
String[] sArr = s1.split(",");
String finalStr = new String();
for(String s : sArr){
finalStr = finalStr +" "+ s;
}
System.out.println(finalStr);
}
}