Validate string a+b - java

I would like to validate if the particular string is true or not in form of a + b
If input = a + b true
If input = a + false
if input = + b false
where a and b can be any string characters

I can think of a couple of ways:
Use a regex to match a "+" the characters before and after it.
Use String.indexOf("+") to find a "+" character and test the value of the index to see if it as the start or end of the string.
(Don't forget the cases where a or b could contain a "+" character; i.e. multiple "+" characters in the string.)

You can use regular expression (regex) to test the string. In java you can use the Pattern and Matcher classes to test if a string matches a given regex. The regex you want to use is:
String regex = ".* \\+ .*";
This regex will test for a string in the following form: "[characters] + [characters]".
Here is more information about the regex in java.

Related

Java. Regular Expressions. How to mix NOT with AND?

I have a string (VIN) like this:
String vin = "XTC53229R71133923";
I can use OR to see if there are characters Q,O,I:
String regExp = ".*[QOI].*";
This works.
However I can not check that any of these 3 letter are NOT in the string.
It means: (NOT Q) AND (NOT O) AND (NOT I).
I tried negative lookahead:
String regExp = "(?!.*[QOI].*)";
This doens't work. In "XTC5Q3229R71133923" it returns true.
The main issue - I have 2 conditions:
Number of characters (A-Z0-9) in the string should be 17.
The string should not have Q,O,I.
I can check this with 2 regexps:
String regExp = "^([A-Z0-9]{17})$"; //should be true
String regExp = ".*[QOI].*"; //should be false
But is there a way to combine these 2 checks in one regular expression?
How about just using a custom range that doesn't include the characters you do not want?
String regexp = "^([A-HJ-NPR-Z0-9]{17})$";
Here you go ^[^QOI]{17}$. Starting a charcter class with ^ means "do not match any of these characters".

replace a String (in some cases) in java

if i have for example String a = "8sin(30)+sin(40) + 3sin(30)"
String b = a;how I can replace only the first "sin" and the third for "*sin", mantaining the second "sin" the same?
in other word, how I can replace part of a string only in specific cases?
If you want to replace sin which have number before it you can use something like
yourString = yourString.replaceAll("(\\d+)sin","$1*sin");
replaceAll uses regular expression which represents
\\d+ string build from one or more digit characters (like 0, 12, 321...) - we will place this number in group 1
sin literal.
In replacement we are reusing match from group 1 via $1
Demo:
String a = "8sin(30)+sin(40) + 3sin(30)";
System.out.println(a.replaceAll("(\\d)+sin","$1*sin"));
Output: 8*sin(30)+sin(40) + 3*sin(30)
You can also use look-behind to check if before sin there is any digit
String a = "8sin(30)+sin(40) + 3sin(30)";
System.out.println(a.replaceAll("(?<=\\d)sin","*sin"));
You are probably looking for the replaceFirst(String regex, String replacement) method of String.
You can use below regex:
[0-9]+sin
Demo
b=b.replaceFirst("[0-9]+sin","*sin");
Also you can use the substring(int beginIndex) method.

When the following Regex matches?

I found the following regex in one of the Android Source file:
String regex = "\\s+(?i)src=\"cid(?-i):\\Q" + attachment.mContentId + "\\E\"";
if(string.matches(regex)) {
Print -- Matched
} else {
Print -- Not Found
}
NOTE: attachment.mContentId will basically have values like C4EA83841E79F643970AF3F20725CB04#gmail.com
I made a sample code as below:
String content = "Hello src=\"cid:something#gmail.com\" is present";
String contentId = "something#gmail.com";
String regex = "\\s+(?i)src=\"cid(?-i):\\Q" + contentId + "\\E\"";
if(content.matches(regex))
System.out.println("Present");
else
System.out.println("Not Present");
This always gives "Not Present" as output.
But when I am doing the below:
System.out.println(content.replaceAll(regex, " Replaced Value"));
And the output is replaced with new value. If it is Not Present, then how could replaceAll work and replace the new value? Please clear my confusions.
Can anybody say what kind of content in string will make the control go to the if part?
String regex = "\\s+(?i)src=\"cid(?-i):\\Q" + attachment.mContentId + "\\E\"";
Break it down:
\\s+ - Match 1 or more spaces
(?i) - Turn on case-insensitive matching for the subsequent string
src=\"cid - match src="cid
(?-i) - Turn off case-insensitive matching
: - Obviously a colon
\\Q - Treat all following stuff before \\E as literal characters,
and not control characters. Special regex characters are disabled until \\E
attachment.mContentId - whatever your string is
\\E - End the literal quoting sandwich started by \\Q
\" - End quote
So it will match a string like src="cid:YOUR-STRING-LITERAL"
Or, to use your own example, something like this string will match (there are leading white space characters):
src="cid:C4EA83841E79F643970AF3F20725CB04#gmail.com"
For your update
The problem you're running into is using java.lang.String.matches() and expecting it does what you think it should.
String.matches() (and Matcher) has a problem: it tries to match the entire string against the regular expression.
If you use this regex:
String regex = "\\s+(?i)src=\"cid(?-i):\\Q" + attachment.mContentId + "\\E\"";
And this input:
String content = "Hello src=\"cid:something#gmail.com\" is present";
content will never match the regex because the entire string doesn't match the regular expression.
What you want to do is use Matcher.find - this should work for you.
String content = "Hello src=\"cid:something#gmail.com\" is present";
String contentId = "something#gmail.com";
Pattern pattern = Pattern.compile("\\s+(?i)src=\"cid(?-i):\\Q" + contentId + "\\E\"");
Matcher m = pattern.matcher(content);
if(m.find())
System.out.println("Present");
else
System.out.println("Not Present");
IDEone example: https://ideone.com/8RTf0e
That regex will match any
src="cid:contentId"
where only contentId needs to match case sensitive.
For instance giving your example contentId (C4EA83841E79F643970AF3F20725CB04#gmail.com) these strings will match:
SrC="CiD:C4EA83841E79F643970AF3F20725CB04#gmail.com"
src="cid:C4EA83841E79F643970AF3F20725CB04#gmail.com"
SRC="CID:C4EA83841E79F643970AF3F20725CB04#gmail.com"
while these will not match:
src="cid:c4Ea83841e79F643970aF3f20725Cb04#GmaiL.com"
src="cid:C4EA83841E79F643970AF3F20725CB04#GMAIL.COM"
Also the contentId part is escaped (\Q ... \E) so that the regex engine will not consider special characters inside it.

Eliminating Unicode Characters and Escape Characters from String

I want to remove all Unicode Characters and Escape Characters like (\n, \t) etc. In short I want just alphanumeric string.
For example :
\u2029My Actual String\u2029
\nMy Actual String\n
I want to fetch just 'My Actual String'. Is there any way to do so, either by using a built in string method or a Regular Expression ?
Try
String stg = "\u2029My Actual String\u2029 \nMy Actual String";
Pattern pat = Pattern.compile("(?!(\\\\(u|U)\\w{4}|\\s))(\\w)+");
Matcher mat = pat.matcher(stg);
String out = "";
while(mat.find()){
out+=mat.group()+" ";
}
System.out.println(out);
The regex matches all things except unicode and escape characters. The regex pictorially represented as:
Output:
My Actual String My Actual String
Try this:
anyString = anyString.replaceAll("\\\\u\\d{4}|\\\\.", "");
to remove escaped characters. If you also want to remove all other special characters use this one:
anyString = anyString.replaceAll("\\\\u\\d{4}|\\\\.|[^a-zA-Z0-9\\s]", "");
(I guess you want to keep the whitespaces, if not remove \\s from the one above)

java matches regex for variables

I want to match any string that has the pattern
{"id":"362237-
any number of characters followed by
"http//:www.abc.com"
any number of characters followed by
"id":"364121-
any number of characters followed by
"http://www.efg.com"
I want to match above pattern to the string below.
[{"id":"362237-13","http//:www.abc.com"},{"id":"364075-13","http://www.xyz.com"},{"id":"364121-13","http://www.efg.com"}]
Code:
String pttrn=".*{\"id\":"362237-.*\"http//:www.abc.com\".*\"id\":"364121-.*\"http://www.efg.com\".*";
String mtchr="[{\"id\":\"362237-13\",\"http//:www.abc.com\"},{\"id":\"364075-13\",\"http://www.xyz.com\"},{\"id\":\"364121-13\",\"http://www.efg.com\"}]";
boolean b = Pattern.matches(pttrn, mtchr);
System.out.println("b is !!" + b);
I was expecting b to be true but it returns false. I have got the regex wrong.
Please let me know how to fix it.
Thanks
You need to escape your curly brace to the regex engine with a backslash. ...and you'll need to escape the backslash to Java with another backslash.
String pttrn=".*\\{\"id\":\"362237-.*\"http//:www.abc.com\".*\"id\":\"364121-.*\"http://www.efg.com\".*";
String mtchr="[{\"id\":\"362237-13\",\"http//:www.abc.com\"},{\"id\":\"364075-13\",\"http://www.xyz.com\"},{\"id\":\"364121-13\",\"http://www.efg.com\"}]";
boolean b = Pattern.matches(pttrn, mtchr);
System.out.println("b is !!" + b);

Categories