Regex Java when we have specific text upto a pattern - java

As i haven't much worked on regex, can someone help me out in getting the answer for below thing:
(1)I want to remove a text say Element
(2)It may of may not followed by delimiter say pipe(||)
I tried below thing, but it is not working in the way i want:
String str = "String:abc||Element:abc||Value:abc"; // Sample text 1
String str1 = "String:abc||Element:abc"; // Sample text 2
System.out.println(str.replaceFirst("Element.*\\||", ""));
System.out.println(str1.replaceFirst("Element.*\\||", ""));
Required output in above cases:
String:abc||Value:abc //for the first case
String:abc //for the second case

Assuming that you can decide to give another value to the original pattern which is Element in this case, you can use Pattern.quote to escape it as below:
String str = "String:abc||Element:abc||Value:abc"; // Sample text 1
String str1 = "String:abc||Element:abc"; // Sample text 2
String originalPattern = "Element";
String pattern = String.format("\\|{2}%s[^\\|]+", Pattern.quote(originalPattern));
System.out.println(str.replaceFirst(pattern, ""));
System.out.println(str1.replaceFirst(pattern, ""));
Your patter is then generic and its value is String.format("\\|{2}%s[^\\|]+", Pattern.quote(originalPattern))
Output:
String:abc||Value:abc
String:abc

You put the escape wrong. It should be:
Element(.*?\|\||.*$)
Put the escape on each pipe, and use ? for non greedy Regex so you only replace just enough string, not everything.

String text = "String:abc||Element:abc||Value:abc";
text = text.replaceAll("\\belement\\b", "");
you might need to use replace all this will replace all element from your string here i am using '\b' word boundary in java regular expression in between the words

Related

Regex : Digits followed by only one character

I'm trying to create a regex that will allow only digits followed by only one character after every digit within a Textfield
Regex that needs to match - \d*\+{1}
Regex in case it does not match - [^\d*\+){1}] will replace with "" (removes everything else)
final String regexFinalInteger = "\\d*\\+{1}";
numberElements.textProperty().addListener((observable, oldValueE, newValueE) -> {
if (!newValueE.matches(regexFinalInteger)) {
numberElements.setText(newValueE.replaceAll("[^\\d*\\+){1}]", ""));
}
});
I will expect an output of 122+1+3 but the actual output can be 1++2+++4+123 (multiple +)
If I understood correctly, you want to replace multiple +s with only one.
I believe this would enable what you're looking for:
String regex = "[+](?=[+])";
String text = "122+1+3";
assertEquals("122+1+3", text.replaceAll(regex, ""));
text = "1++2+++4+123";
assertEquals("1+2+4+123", text.replaceAll(regex, ""));
That is my first Java program, I'm sorry if it offends someone.

Remove all characters before special character in android

I have to remove ## before $$ from string ##$$abxcyhshbhs##xcznbx##. I am using:
string.split("\\#");
The problem is that it also removes # after $$.
Use replace() instead.
String text = "##$$abxcyhshbhs##xcznbx##";
text = text.replace("##$$", "$$");
You can use substring method like below
string.substring(2);
If you really want to use String.split() you can do what you want by limiting the number of results by doing:
String str = "##$$abxcyhshbhs##xcznbx##";
str = str.split("##", 2)[1];
I don't know your exact issue but as has already been said, replace() or substring() is probably a better option.
If you have unknown number of # symbols before $$ and they appear not just at the beginning of the string, you can use the following replaceAll with a regex:
String re = "#+\\${2}";
String str = "##$$abxcyh###$$shbhs##xcznbx##";
System.out.println(str.replaceAll(re, "\\$\\$")); // Note escaped $ !!!
// => $$abxcyh$$shbhs##xcznbx##
// or
re = "#+(\\${2})"; // using capturing and back-references
System.out.println(str.replaceAll(re, "$1"));
See IDEONE demo.
Do not forget to assign the variable a new value when using in your code:
str = str.replaceAll("#+(\\${2})", "$1")
If your purpose is to remove ## from first occurrence of ##$$ in the string, then following code snippet will be helpful:
if(yourString.startsWith("##$$")){
yourString.replaceFirst("##$$","$$");
}
OR considering there is only single $$ in your string, following would be helpful:
String requiredString="";
String[] splitArr = yourString.split("\\$");
if ( splitArr.length > 1 ) {
requiredString = "$$" + splitArr[splitArr.length-1];
}
I have written a code snippet here. You can make changes and execute on your own.
To literally remove the first two characters, use the following:
String s = "##$$abxcyhshbhs##xcznbx##";
s.substring(2, s.length());
This doesn't do any pattern matching to look for the $$.

JAVA: Replacing words in string

I want to replace words in a string, but I am having little difficulties. Here is what I want to do. I have string:
String a = "I want to replace some words in this string";
It should work like some kind of a translator. I am doing this with String.replaceAll(), but it doesn't work completely because of this. Let's say I am translating from English to German, than this should be the output (Ich means I in German).
String toTranslate = "I";
String translated = "Ich";
a = a.replaceAll(toTranslate.toLowerCase(), translated.toLowerCase());
Now the output of the String a will be this:
"ich want to replace some words ich**n** **th**ich**s** **str**ich**ng**"
How to replace just the words, not the subwords in the words?
replaceAll uses regex, so you may add word boundaries or look-around mechanisms to check if there are no non-space characters surrounding word you want to replace.
String toTranslate = "I";
String translated = "Ich";
a = a.replaceAll("(?<!\\S)"+toTranslate.toLowerCase()+"(?!\\S)", translated.toLowerCase());
You can also add quotation mechanism to escape any regex metacharacters like + * ( inside word you want to replace. BTW you don't need to change your string to lower case, simply add case-insensitive flag to regex (?i).
a = a.replaceAll("(?i)(?<!\\S)"+Pattern.quote(toTranslate)+"(?!\\S)", translated.toLowerCase());
Use split(" ") for getting each word in the sentence. And then use replaceAll on each word.
String a = "I want to replace some words in this string";
String toTranslate = "I";
String translated = "Ich";
String newString[]=a.split(" ");
for (String string : newString) {
string=string.replaceAll(toTranslate, toTranslate.toLowerCase());//Adding this line ensures you dont miss any uppercase toTranslate
string=string.replaceAll(toTranslate.toLowerCase(), translated.toLowerCase());
System.out.println("after translation ="+string);
}
String toTranslate = "I ";
String translated = "Ich ";
a = a.replaceAll(toTranslate.toLowerCase(), translated.toLowerCase());
If you add a space after the "I" it should replace it when it comes to the word "Ich" but if your word ends in a "I" then thats another problem
If you assume that I will always be capitalized in English as it should be then
a = a.replaceAll(toTranslate, translated);
will work, otherwise you need to replace both cases
a = a.replaceAll(toTranslate, translated);
a = a.replaceAll("([^a-zA-Z])("+toTranslate.toLowerCase()+")([^a-zA-Z])", "$1"+translated.toLowerCase()+"$3");
Here is a working example
Yes, the word boundaries are the solution. I just did this in the regex:
text.replaceAll("\\b" + parts1[i] + "\\b", map.element.value);
Don't be confused with the second argument it's string (from Hash table).
You can use RegEx's word bound, which is \b
String toTranslate = "\\bI\\b";
String translated = "Ich";
a = a.replaceAll(toTranslate.toLowerCase(), translated.toLowerCase());
This should ensure I is separated entirely into its own word
Edit: I misread the question and realized you want whole words. See above, as I have accounted for that

Unable to replace String containing special characters

I want to replace all special characters with whitespace but I am unable to replace x :
String search = "640×20141007151608##$%$20141008104817.jpeg";
String newSearch = search.replaceAll("[\\p{Punct}&&[^_]]", "");
System.out.println(newSearch);
output : 640×2014100715160820141008104817jpeg
I use the logic below:
String newSearch = search.replaceAll("[^A-Za-z0-9 ]","");
That is, remove anything that is not a number or a digit. Is this what you wanted ?
[^0-9a-zA-Z\.]
Try this.Repalce by ``.See demo.
http://regex101.com/r/hQ1rP0/51

Regex: extract String from String

I need a regex that makes it possible to extract a part out of String. I get this String by parsing a XML-Document with DOM. Then I am looking for the "§regex" part in this String and now I try do extract the value of it. e.g. "([A-ZÄÖÜ]{1,3}[- ][A-Z]{1,2}[1-9][0-9]{0,3})" from the rest.
The Problem is, I don´t know how to make sure the extracted part ends with a ")"
This regex needs to work for every value given. The goal is to write only the Value in brackets after the "§regex=" including the brackets into a String.
<UML:TaggedValue tag="description" value=" random Text §regex=([A-ZÄÖÜ]{1,3}[- ][A-Z]{1,2}[1-9][0-9]{0,3}) random text"/>
private List<String> findRegex() {
List<String> forReturn = new ArrayList<String>();
for (String str : attDescription) {
if (str.contains("§regex=")) {
String s = str.replaceAll(regex);
forReturn.add(s);
}
}
return forReturn;
}
attDescription is a list which contains all Attributes found in the XML-Document parsed.
So far i tried this regex: ".*(§regex=)(.*)[)$].*", "$2" but this cuts off the ")" and does not delete the text infront of the searched part. Even with the help of this http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html I really don´t understand how to get what I need.
It seems to work for me (with this example anyway) if I use this in place of String s = str.replaceAll(regex);
String s = str.replaceAll( ".*§regex=(\\(.*\\)).*", "$1" );
It's just looking for a substring enclosed by parentheses following §regex=.
This seems to work:
String s = str.replaceAll(".*§regex=\\((.*)[)].*", "$1");
Note:
Escape the leading bracket
The $ inside a character class is a literal $ - ignore it, because your regex should always end with a bracket
No need to capture the fixed text
Test code, noting that this works with brackets in/around the regex:
String str = "random Text §regex=(([A-ZÄÖÜ]{1,3}[- ][A-Z]{1,2}[1-9][0-9]{0,3})) random text";
String s = str.replaceAll(".*§regex=\\((.*)[)].*", "$1");
System.out.println(s);
Output:
([A-ZÄÖÜ]{1,3}[- ][A-Z]{1,2}[1-9][0-9]{0,3})

Categories

Resources