I am having regex expression problem. need helps from regex experts!
It's fairly simple but I can't get it to work.
I know if I want to check the starting of a text, I should use ^
and ending of the text, I should use $
I want to replace [quote] to <a>quote</a>.
This doesn't seems to work..
String test = "this is a [quote]"
test.replaceAll("^\\[", "<a>");
test.replaceAll("\\]$", "</a>");
I want the string to become "this is a <a>quote</a>"..
If you want to replace [ and ] with pair, you need to replace them in one time.
String test = "this [test] is a [quote]";
String result = test.replaceAll("\\[([^\\]]+)\\]", "<a>$1</a>");
^ implies that you are looking for something at the beginning of the string. However [ does not appear at the beginning of the string, so you will not have a match. Just do:
test.replaceAll("\\[", "<a>");
test.replaceAll("\\]", "</a>");
Also, you cannot modify a String in-place. you'll have to assign the output to something. You can do:
test = test.replaceAll("\\[", "<a>").replaceAll("\\]", "</a>");
That is if you still want to use the variable test.
Related
I have the following requirement where in I need to do few things only if the given string ends in "Y" or "Years" or "YEARS".
I tried doing it using regex like this.
String text=1.5Y;
if(Pattern.matches("Y$",text) || Pattern.matches("YEARS$",text) || Pattern.matches("Years",text))
{
//do
}
However this is getting failed.
Can someone point me where I have gone wrong or suggest me any other feasible method.
EDIT:
Thanks.That helps.
Finally I have used "(?i)^.*Y(ears)?$| (?i)^.*M(onths)?$".
But I want to make more changes to make it perfect.
Let's say I have many strings.
Ideally only strings like 1.5Y or 0.5-3.5Y or 2.5/2.5-4.5Y should pass if check.
It can be number of years(Ex:2.5y) or the period of years(2.5-3.5y) or the no of years/period of years(Ex.2.5/3.5-4.5Y) nothing more.
More Examples:
--------------
Y -should fail;
MY - should fail;
1.5CY - should fail;
1.5Y-2.5Y should fail;
1.5-2.5Y should pass;
1.5Y/2.5-3.5Y should fail;
1.5/2.5-3.5Y should pass;
You don't need a regex here:
if(text.endsWith("Y") || ...)
matches method attempts to match full input so use:
^.*Y$
for your first pattern.
btw you can use a single regex for all 3 cases:
if (text.matches( "(?i)^.*Y(ears)?$" ) ) {...}
(?i) does ignore case match.
.*(?:Y|YEARS|Years)$
You can directly use this .Match matches from beginning.So yours is failing.
You can simply use the regex pattern:
if (Pattern.matches(".*(Y|YEARS|Years)$",text)) {/*do something*/}
/((?!0)\d+|0)(.\d+)?(?:years|year|y)/gi
https://regex101.com/r/gJ6xD2/2
var text = "1.6y 1.5years 1year 1.5h";
text.match(/((?!0)\d+|0)(\.\d+)?(?:years|year|y)/gi);
Result["1.6y", "1.5years", "1year"]
(?=^(0\.\d+|[1-9](?:\d+)?(?:\.\d+)?)(?:(\s+)?[\/-](\s+)?(?:0\.\d+|[1-9](?:\d+)?(?:\.\d+)?))*(?:\s+)?(?:y(?:(ea)?rs|ears?)?|m(?:onths?)?)$).*
https://regex101.com/r/kL7rQ1/3
Only thing I wasn't sure "2.3 - 4 / 6.2 y" format is acceptable or not, so I've included it.
I have following two different payloads where I am trying to Write Java Regex:
Input Payload 1
ISA*00* *00* *ZZ*EXDO *ZZ*047336389 *150327*1007*U*00401*900063730*0*P*>~
GS*QM*EXDO*047336389*20150327*1007*900063730*X*004010~
ST*214*900063730~
B10*326GENT15173**EXDO~
L11*019*TN~
Input Payload 2
ISA*00* *00* *02*HJBT *01*047336389 *140103*1751*U*00401*000012003*0*P*>\
GS*QM*HJBT*047336389*20140103*1751*12003*X*004010\
ST*214*0001\
B10*117094*B065199*HJBT\
N1*SH*INTEVA PRODUCTS LLC-\
I have following REGEX:
.*(ST\*214|ST\*210).*
I tried to evaluate the REGEX on this URL http://www.regexplanet.com/advanced/java/index.html
I see matches() as NO for 1st Payload and matches() as YES for 2nd Payload. I am looking for Updated REGEX which actually works for BOTH conditions here.
My Purpose here to validate payload information just like String contains method can do it using following approach.
payload.toString().contains('ST*214') || payload.toString().contains('ST*210').
I want to use regex instead of string.contains here.
"(?s).*(ST\\*214|ST\\*210).*"
In Java you need to enable DOTALL mode (to make . match with line terminators too). This can be done by including (?s) modifier. You had match only in this ST*214*900063730~ particular part of first string.
use the following regexp:
".*(ST\*214|ST\*210).*"
Have tested your two strings with following code:
public class RegTest {
public static void main (String[] args) {
String test1 = "ISA*00* 00 ZZEXDO *ZZ*047336389*150414*1108*U*00401*979863647*0*P*>~ GSQMEXDO*047336389*20150414*1108*979863647*X*004010~ ST*214*979863647~ B10*186143**EXDO~";
String test2 = "ISA*00* 00 *02*HJBT *01*047336389*140103*1751*U*00401*000012003*0*P*>\\GSQMHJBT*047336389*20140103*1751*12003*X*004010\\ST*214*0001\\B10*117094*B065199*HJBT\\N1*SH*INTEVA PRODUCTS LLC-\\";
if (test1.matches(".*(ST\\*214|ST\\*210).*")) {
System.out.println("String1 matches");
}
if (test2.matches(".*(ST\\*214|ST\\*210).*")) {
System.out.println("String2 matches");
}
}
}
just small fix, regexp in comment lost two '\' characters. You can use the regexp from code.
I think you try to match the wildcard character '*' so you should use backslashes :
.*(ST\*214|ST\*210).*
or
.*ST\*(214|210).*
or
.*ST\*21(4|0).*
or
.*ST\*21[40].*
Are the linefeed part of your payload or just some formatting ?
Why does this code not work?
public static void main(String[] args) {
String s = "You need the new version for this. Please update app ...";
System.out.println(s.replaceAll(". ", ".\\\\n").replaceAll(" ...", "..."));
}
This is my wanted output:
You need the new version for this.\nPlease update app...
Thanks for the information
String.replaceAll method takes Regex as first argument.
So you need to escape your dot (.), as it has special meaning in Regex, which matches any character.
System.out.println(s.replaceAll("\\. ", ".\\\\n").replaceAll(" \\.\\.\\.", "..."));
However, for your given input, you can simply use String.replace method, as it does not take Regex, and has an added advantage of that.
. is a special regex character and will match anything. You need to escape it like this: \\.
So to match three dots you must use following regex: "\\.\\.\\."
what you want is
s.replaceAll("\\. ", ".\n").replaceAll(" \\.\\.\\.", "...")
You shouldn't be using replaceAll - use replace instead. replaceAll takes a regular expression when it is not needed here (and hence it will be unnecessarily inefficient).
String s = "You need the new version for this. Please update app ...";
System.out.println(s.replace(". ", ".\\n").replace(" ...", "..."));
(Also note that I've replaced ".\\\\n" with ".\\n" here, which produces the desired output.)
try as
System.out.println(s.replace(". ", ".\n").replace(" ...", "..."));
this gives
You need the new version for this.
Please update app...
I have seemingly simple task, but I have no experience with regular expressions.
I have to parse SMS body with predefined message text, to get out certain information.
Here is one example:
Täname! {FirstName} {LastName} isikukoodiga {PersonCode} on sõlminud EMT Reisikindlustuse lepingu numbriga {PolicyNumber}, mis kehtib alates {CoverStartDate} kell {CoverStartTime} kuni {CoverEndDate} kell {CoverEndTime} (Eesti aja järgi). Hind: {PremiumEur} eurot. Tutvu tingimustega ({Terms}) http://emt.ee/kindlustus. Kahjukäsitluse number +3727330700.
I have to parse out everything that is in curly braces.
I came up with something like this in Java:
public static final String REGEX_CONFIRMATION = "Täname! (.*) (.*) isikukoodiga (.*) on sõlminud EMT Reisikindlustuse lepingu numbriga (.*), mis kehtib alates (.*) kell (.*) kuni (.*) kell (.*) \\(Eesti aja järgi\\). Hind: (.*) eurot. Tutvu tingimustega \\((.*)\\) http://emt.ee/kindlustus. Kahjukäsitluse number \\+3727330700.";
But it parses out only following groups:
{MARIS}, {PLOTS}, {17204046521}, {22414152}, {01.10.2002}, {13:07},
{02.10.2002}, {23:59}.
As you can see {Terms} is missing. And I can't seem to figure out where is the problem?
how about using this pattern?
\{.*?\}
Wouldn't it make more sense to simply use
\{[^{}]*\}
as your regex? In a string, you would need to write that as
"\\{[^{}]*\\}"
Explanation:
\{ # Match an opening brace
[^{}]* # Match any number of characters except braces
\} # Match a closing brace
http://www.java2s.com/Code/Java/Regular-Expressions/Findallmatches.htm
along with the following regex
\{(.*?)\}
Seems correct to me. Use the DOTALL (and in other cases maybe MULTILINE) options. DOTALL can be added as "(?s)Täname!...". Then the ".*" also maps newline chars.
As the prior matches were found this might be it.
Does it work, when You include brackets into your {TERMS} part?
Instead of:
String regex = "...Tutvu tingimustega \\((.*)\\) http://emt.ee/kindlustus. ...";
You could try:
String regex = "...Tutvu tingimustega (.*) http://emt.ee/kindlustus. ...";
OR depending on, what You have in {TERMS} string, You could change _.*_ to _[^)]*_
This way you would find zero to N chars that are not ending bracket.
I have following code in Java:
Pattern fieldsPattern = Pattern.compile("(\"([^\"]+)\")|"
+"("+this.field_tag+"([0-9a-zA-Z_]+))");
Matcher fieldsMatcher = fieldsPattern.matcher(field);
while(fieldsMatcher.find())
{
//...
}
This code should capture expressions like "expression" and :expression (field_tag is just ":"). The problem occurs when I try to capture an expression like: "10.1" or "10,1". It dosen't work.
But expressions:
"10-1",
"10+1"
works as expected.
I also tried use this regexp on regexpal.com - site with javascript implementation of RegExp. On this site expressions like "10.1" and "10,1" works fine.
Is there any difference in java vs javascript in capturing dots? What am I doing wrong?
This works for me
Pattern fieldsPattern = Pattern.compile("(\"[^\"]+\")");
String field =" aa \"10\" \"10.1\" and \"10,1\"";
Matcher fieldsMatcher = fieldsPattern.matcher(field);
while(fieldsMatcher.find()) {
System.out.println(fieldsMatcher.group());
}
prints
"10"
"10.1"
"10,1"
The second set of brackets in the regex appear to be redundant, but are harmless.