Matching a single word inside optional parentheses - java

I am using java regex to match a specific platform.
For example, my list of strings are:
Ruby 2.1 (Puma)
Ruby 2.3 (Passenger Standalone)
Ruby 2.2 (Puma)
Ruby 2.2 (Passenger Standalone)
Ruby 1.9.6
From these I want to only pick the ones which are not Passenger standlone. ie. Ruby 2.1 (Puma) and Ruby 2.2 (Puma) and Ruby 1.9.6. The only restriction/condition is that the version name i.e Puma or Passenger Standalone is optional. And therefore the parenthesis and the string inside the parenthesis are optional
I am trying to use the following regex:
"Ruby (.*)\s+\(?\w{0,1}\)?$"
I have the options as an ArrayList of Strings. My method code looks like:
public void displayVersions(String platform) {
String regex = "Ruby (.*)\s+\(?\w{0,1}\)?$";
List<String> availableVersions = getAvailableVersions();
List<String> filtered = availableVersions.stream()
.filter(version -> version.matches(regex))
.collect(Collectors.toList());
System.out.println(filtered);
}
The regex is incorrect and I am not getting the correct results. I get the Passenger Standalone versions only. What am I doing wrong?

Fixed regex: Ruby ([0-9.]+)\s?\(?[^\s]*\)?$
You may also need to add /m option if the string to match is multiline
I personally suggest you https://regex101.com to check and debug regexes. For example https://regex101.com/r/c148v8/2

Ruby [0-9.]+ \((\S)\)
This will do the trick.
Ruby [0-9.]+ matches the ruby version
\((\S)\) matches any non-whitespace char within parenthesis.

Use nested optionals:
^Ruby \d+(\.\d+)*( \(?\w+\)?)?$
See live demo.

Related

Validation of Conditional String Android

I have been trying to validate a sample string which I read from a file. I want to check if the condition in the given string evaluates to true or false.
String Test = "( ((10>20) & (10>5)) & (7>9) ) | (123>45)";
How can I do that using Java. I have been trying to split the brackets and operators. Is there any easy way to solve this kind of textual expressions?
I have tried JSR.JAR but I'm getting
java.lang.NoClassDefFoundError: Failed resolution of: Lsun/misc/Service; at javax.script.ScriptEngineManager.initEngines(ScriptEngineManager.java:108)at javax.script.ScriptEngineManager.access$000(ScriptEngineManager.java:55)at javax.script.ScriptEngineManager$1.run(ScriptEngineManager.java:98)at java.security.AccessController.doPrivileged(AccessController.java:45)at javax.script.ScriptEngineManager.init(ScriptEngineManager.java:96)at javax.script.ScriptEngineManager.(ScriptEngineManager.java:69)
It seems that android does not provide the java scripting engine.
Possibly you could use a third party library instead:
AndroidJSCore
J2V8 (java wrapper built on top of googles V8 engine)

Jenkins Console section: What Java regex will trigger on string ERROR but not on string %%ERRORLEVEL%%?

I am using the Jenkins console sections plugin [1] on a windows server. It is excellent in order to make a nice left navbar on my logs.
Positively, I would like any error message to cause a section header, eg;
Assert-PathExstsNotTooLong : ERROR, The path does not exist: E:\P...
...
Oops! Error, please do not do that.
Negatively, I would like to be able to avoid having spelled-out execution templates cause a new section header, eg the below.
[workspace] $ cmd.exe /C " c:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe /p:Configuration=Debug /p:VisualStudioVersion=12.0 "E:\Program Files (x86)\Jenkins\jobs\M.sln"
Using references here on SO [2] and on the tester you recommended [3], I came up with the following, but it is not working?
^(?=(.*([Ee][Rr][Rr][Oo][Rr] ).*))(?!(%%ERRORLEVEL%%))
Using Regex101's amazing tester, with JS flavor, I used the above as input and had these test strings and outputs. The second line of match info perhaps explains my issue but I do not understand it.
test-strings =
help error you should see me
i am %%errorlevel%% again
i am not a section
match-info;
1. `help error you should see me`
2. `error `
Any tips?
thank you!
1.[] ;This plugin uses Java Regex, per its docs ; ; ; ; X.Collapsing Console Sections Plugin - Jenkins - Jenkins Wiki ; ; https://wiki.jenkins-ci.org/display/JENKINS/Collapsing+Console+Sections+Plugin
2.[] ; An example regex on characters, not strings, to avoid; ; ; ; X.java - Regular expression include and exclude special characters - Stack Overflow ; ; Regular expression include and exclude special characters
3.[] ; ; ; ; ; X.Online regex tester and debugger: JavaScript, Python, PHP, and PCRE ; ; https://www.regex101.com/#javascript
(I can't add comments yet, otherwise I'd ask directly, but your example of a spelled-out message template doesn't include the text %%ERRORLEVEL%%, but I assume that it's meant to be a string with %%ERRORLEVEL%% somewhere in the middle of it. Also, as the example isn't quite right, I can't tell exactly what you mean by "not working")
Your problem is that your regex matches ERROR_ (with a space) anywhere in the text, except where the text is exactly %%ERRORLEVEL%%. I think that instead you could write:
^(?=(.*([Ee][Rr][Rr][Oo][Rr])))(?!.*(%%ERRORLEVEL%%)).*
Do you really need to only match ERROR_ (with a space) as opposed to ERROR (whether or not it has a space)? If the former, then you are already excluding %%ERRORLEVEL%%, and you could just use .*(?i:ERROR ).* as the full regex.
The Collapsing Console Sections Plugin uses Java regular expressions, so you can use (?i:ERROR) to match ERROR case-insensitively.
You need a trailing .* before and after your negative-lookahead atom for %%ERRORLEVEL%%, otherwise it will only exclude an exact match
The documentation for the plugin doesn't say whether the pattern has to match a line completely, or if it just matches text within the line. If it matches the line completely, the leading ^ is unnecessary, but won't be doing any harm.
You've got capturing brackets around ERROR and %%ERRORLEVEL%%. If you're not doing anything with that text, then those brackets are unnecessary.
The following regex will match any line with any of ERROR, Error, error etc in it, except lines with any of %%ERRORLEVEL%%, %%ErrorLevel%%, %%errorlevel%% etc.
^(?=.*(?i:ERROR))(?!.*(?i:%%ERRORLEVEL%%)).*

Regular Expression in burp

I'm using a forward proxy called Burp and would like to see only results from google in my site scope.
What will be the regex for if i want to see *.google.* in my result
So sample output can be
www.google.com
drive.google.com
google.in
and so on
This should work for you:
^.*?google\..*$
Will match anything before and after .google.
^.*\.domain\.com$
^.*\.test\.domain\.com$
^ -> Signifies beginning of the regex
.* -> accept anything
. -> Escape sequence for dot
$ -> End Regex

RegEx to match ends with

I need to write regex in java to match domain and subdomain(.domain.com).
Regex should return true for
domain.com
m.domain.com
abc.domain.com
www.domain.com
but returns false for
abcdomain.com
1domain.com
I try to match domain.com and and if preceding character is present then it must be .
I tried various options but it is failing in one or other test cases.
(^|.*?\.)domain\.com
Try this. See demo.
http://regex101.com/r/lB2sH2/1
Try this:
(\.|^)domain.com$
The first part means that there should be a . or nothing
and the $ means, "ends with"
You can try:
(^|\.)domain\.com$
but Java mostly handles only full-line matches, so:
(.+\.)?domain\.com
or you can use the .endWith() method in Java code:
if (domain.equals("domain.com") || domain.endsWith(".domain.com")) {
// do something...
}
I think you want something like this,
(?:\\w+\\.?)?domain\\.com
DEMO
try this regex
\bdomain\.com$
http://rubular.com/r/QG0FtVWtm6
If you don't know what "domain.com" is going to be, this regex below should give you just the subdomain of whatever domain you are looking for. Matches your specifications, including domains that look like abc.net
([a-z]+)(?=\.[a-z]+\.)
DEMO

Capturing dot and comma in Java RegExp

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.

Categories