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.
Related
I had asked a question , many thanks for all the help.
I have a URL Like this.
/Hello/World/special/Case/2016/07/01/offer-015155.html
I need only "2016/07/01/offer-015155" this part and this dynamically changes each time. Could you help?
I tried "(.*?)" , "\d{4}/\d\d/\d\d/offer-\d+." but did not help.
When I run it says, not found . :(
If you want extract the part of an URL it will be quite enough to use something very simple like (.+)
Demo:
Example Regular Expression configuration (mind "Field to Check" bit)
References:
JMeter Regular Expressions
Perl 5 Regex Cheat sheet
Using RegEx (Regular Expression Extractor) With JMeter
You could consider using a positive lookbehind combined with a positive lookahead, like this:
(?<=\/Hello\/World\/special\/Case\/).*(?=.html)
This regex is very explicit but does the job. See this for an explanation of the regex: https://regex101.com/r/vY5mS2/2
EDIT:
Or simply use capture groups (https://regex101.com/r/vY5mS2/5)
\/Hello\/World\/special\/Case\/(.*)\.html
You can try the following code also:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("(\\d{4}/\\d{2}/\\d{2}/offer-\\d+)");
String testString = "/Hello/World/special/Case/2016/07/01/offer-015155.html";
Matcher matcher = pattern.matcher(testString);
if (matcher.find()) {
System.out.println(matcher.group());
}
}
}
Execution result: 2016/07/01/offer-015155
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.
We're using GWT 2.03 along with SmartGWT 2.2. I'm trying to match a regex like below in client side code.
Pattern pattern = Pattern.compile("\\\"(/\d+){4}\\\"");
String testString1 = "[ \"/2/4/5/6/8\", \"/2/4/5/6\"]";
String testString2 = "[ ]";
Matcher matcher = pattern.matcher(testString1);
boolean result = false;
while (matcher.find()) {
System.out.println(matcher.group());
}
It appears that Pattern and Matcher classes are NOT compiled to Javascript by the GWTC compiler and hence this application did NOT load. What is the equivalent GWT client code so that I can find regex matches within a String ?
How have you been able to match regexes within a String in client-side GWT ?
Thank you,
Just use the String class to do it!
Like this:
String text = "google.com";
if (text.matches("(\\w+\\.){1,2}[a-zA-Z]{2,4}"))
System.out.println("match");
else
System.out.println("no match");
It works fine like this, without having to import or upgrade or whatever.
Just change the text and regex to your liking.
Greetings, Glenn
Consider upgrading to GWT 2.1 and using RegExp.
Use GWT JSNI to call native Javascript regexp:
public native String jsRegExp(String str, String regex)
/*-{
return str.replace(/regex/); // an example replace using regexp
}
}-*/;
Perhaps you could download the RegExp files from GWT 2.1 and add them to your project?
http://code.google.com/p/google-web-toolkit/source/browse/trunk/user/src/com/google/gwt/regexp/
Download GWT 2.1 incl source, add that directory somewhere in your project, then add the reference to the "RegExp.gwt.xml" using the <inherits> tag from your GWT XML.
I'm not sure if that would work, but it'd be worth a shot. Maybe it references something else GWT 2.1 specific which you don't have, but I've just checked out the code a bit and I don't think it does.
GWT 2.1 now has a RegExp class that might solve your problem:
// Compile and use regular expression
RegExp regExp = RegExp.compile(patternStr);
MatchResult matcher = regExp.exec(inputStr);
boolean matchFound = regExp.test(inputStr);
if (matchFound) {
Window.alert("Match found");
// Get all groups for this match
for (int i=0; i<=matcher.getGroupCount(); i++) {
String groupStr = matcher.getGroup(i);
System.out.println(groupStr);
}
}else{
Window.alert("Match not found");
}