regex which should check string contains specified word - java

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+.";

Related

Replace all characters between two delimiters using regex

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)

Java Regex to replace a pattern in a certain string

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, "");
}
}

How can I change the invalid characters to valid chars in Java?

private static void isValidName(String[] filename){
FileSystem fs = FileSystems.getDefault();
System.out.println(fs);
String pattern = ("^[\\w&[^?\\\\/. ]]+?\\.*[\\w&[^?\\\\/. ]]+$");
for (String s: filename) {
//System.out.println(s.matches(pattern));
if (s.matches(pattern)==false){
System.out.println(s.matches(pattern));
}
}
Now I call this function:
String[] name2={"valami.txt."};
isValidName(name2);
How can I replace the invalid characters in if(s.matches(pattern)==false) with valid characters?
Output:
false
You may use this piece of code to remove/replace invalid characters:
String[] bad = {
"foo.tar.gz",
" foo.txt",
"foo?",
"foo/",
"foo\\",
".foo",
"foo."
};
String remove_pattern = "^[ .]+|\\.+$|\\.(?=[^.]*\\.[^.]*$)|[?\\\\/:;]";
for (String s: bad) {
System.out.println(s.replaceAll(remove_pattern, "_"));
}
See IDEONE demo
Output:
foo_tar.gz
_foo.txt
foo_
foo_
foo_
_foo
foo_
REGEX contains several alternatives joined with | alternation operator to match the invalid character(s) only.
^[ .]+ - Matches 1 or more leading spaces or dots
\\.+$ - Matches final ., 1 or more occurrences (change to [. ]+$ if you plan to also replace trailing spaces)
\\.(?=[^.]*\\.[^.]*$) - Matches a . that is followed by an optional number of characters and another dot (thus, leaving the last dot in the string)
[?\\\\/:;] - Matches ?, \, /, : and ; literally.

Java RegExp Replace

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

need regex pattern to split String

How to split the following string
String str = "(obj.userAge EQUALS 51) AND (obj.userAddress CONTAINS STREET1)";
so that I should get
string 1 = "obj.userAge EQUALS 512";
string 2 = "obj.userAddress CONTAINS STREET1";
I tried with
str.split("AND")
but I need string without brackets
sometime I can get a string from the database like
String str = "(obj.userAge EQUALS 51) AND (obj.userAddress CONTAINS STREET1) OR (obj2.salary >= 3000)";
so now OR is added.
Try this:
public static void main(String[] args) {
String str = "(obj.userAge EQUALS 51) AND (obj.userAddress CONTAINS STREET1)";
Pattern pattern = Pattern.compile("\\((.+?)\\)");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
}
Output:
obj.userAge EQUALS 51
obj.userAddress CONTAINS STREET1
You should match it with this regex
(?<=\\().*?(?=\\))
. matches a single character.
* is a quantifier that matches 0 to many preceding character
.*? matches 0 to many characters lazily
(?<=) is a positive lookbehind which checks for a pattern before the current position.So (?<=a)b would match b only if it's preceded by a
(?=) is a positive lookahead which checks for a pattern after the current position.So a(?=b) would match a only if it's followed by b
This regex would generate a group for each of the brackets which you then can retrieve: \((.*)\)\sAND\s\((.*)\)
And yet one more (for the fun of it) using guava:
Iterable<String> result = Splitter.on(Pattern.compile("\\(|\\)|AND")).omitEmptyStrings().trimResults().split(input);

Categories

Resources