RegEx to match ends with - java

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

Related

Need to get Particular word using java Regex

I want to get one particular word using regex in java. thanks
in the below paragraph, I need to find the network interface name
resource "azurerm_network_interface" "nic_LinuxVMCent-nhi" {
name = "nic_LinuxVMCent-nhi"
location = azurerm_resource_group.main.location
resource_group_name = azurerm_resource_group.main.name
ip_configuration {
name = "pubIP_LinuxVMCent-nhi"
subnet_id = azurerm_subnet.sub_wind12VM-PtN.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.pubIP_LinuxVMCent-nhi.id
}
}
data "azurerm_snapshot" "snapLinuxVMCent-nhi" {
name = "CentOS76New-0"
resource_group_name = "SaaSworkloadsnaps"
}
Expected Result Ex:
nic_LinuxVMCent-nhi
This is a multi-line bit of text. However, there appears to be a line which you could recognise with a regex:
resource "azurerm_network_interface" "nic_LinuxVMCent-nhi" {
So the regex for that would be ^resource "azurerm_network_interface" "([^"]+)" {$ - see https://regexr.com/67ldb
You can use Matcher.match to see if the any line matches this expression and if it does then matcher.group(1) will be the value you're looking for.
you can use this regex to find the network interface name:
(?<=resource \"azurerm_network_interface\" \").+(?=\" {)
I have used lookahead to find the name.
Also, here's a link to regex101:
Link
I don't know network interfaces so,
This regex solution is specific to "azurerm_network_interface."
If you need any additional help, please comment down below.
Cheers :)

Finding multiple groups in Java regex for simple option parser

I need to modify this regex to find multiple group matches:
(?:--)(?<key>[^\s=]+)(?:(?<assign> *[ =] *)(?! --)(?<value>"[^"]*"|\S+))?
In Java:
"(?:--)(?<key>[^\\s=]+)(?:(?<assign> *[ =] *)(?! --)(?<value>\"[^\"]*\"|\\S+))?"
This matches the following correctly:
--key=value
--key=--value
--key value
--flag
--key="--value"
--key "--value"
--key=value --foo=bar
--key=value --foo=bar --flag
But it fails if --flag comes before any other options:
--key=value --flag --foo=bar
I've been trying to modify the negative lookahead between the assign and value capture groups without success so far. The value captured for flag ends up being --foo=bar instead of null.
Any expert recommendations on how to solve this?
I managed to fix the regex. The website https://regexr.com/ was invaluable.
The fixed regex is:
(?<prefix>--)(?<key>[^\s=]+)(?:(?! --)(?<assign> *[ =] *)(?! --)(?<value>"[^"]*"|\S+))?
Here's the Java class and unit test:
https://gist.github.com/kirklund/845baf340a1999a57db9e59e6ba40ce0

Android regular expression doesn't match

Here is something that I don't really understand.
I would like to get the date part from the following string:
<th>Elkezdodott</th>
<td>2016. december 20., 19:29</td>
So I use the following code:
System.out.println(html);
Pattern p = Pattern.compile("\\p{Punct}th\\p{Punct}Elkezdodott\\p{Punct}{2}th\\p{Punct}\\p{Space}*" +
"\\p{Punct}td\\p{Punct}" +
"(\\d{4}\\p{Punct}\\p{Space}*[a-zA-Z]*\\p{Space}*\\d*\\p{Punct}{2}" +
"\\p{Space}*\\d{2}\\p{Punct}\\d{2})\\p{Punct}{2}td\\p{Punct}");
Matcher m = p.matcher(html);
if(m.matches()){
System.out.println("matches");
System.out.println(m.group());
}
This regex seems correct according to the Check RegExp option of the Android Studio:
The result of the System.out.println(html) is exactly the same as you can see on the image:
06-03 11:49:15.779 4581-5229/hu.lyra.moly_kihivasok I/System.out: <th>Elkezdodott</th>
06-03 11:49:15.779 4581-5229/hu.lyra.moly_kihivasok I/System.out: <td>2016. december 20., 19:29</td>
What I really don't understand is why m.matches() returns false. I also tried m.find(), but I got the same result. Did I miss something?
Thanks for any advice.
I've executed your exact example and it matches the string. The only thing you did wrong, is not passing an argument to the group() function. You need to define which group you want to match. In your case, this would be the first one. So, use group(1);.
Btw. why are you using such a complicated pattern to match your string? I would not use \p{} that often, because it makes it unreadable. Just use this:
"<th>Elkezdodott</th>\\n<td>(\\d{4}\\.\\s*[a-zA-Z]+\\s*\\d{1,2}\\.,\\s*\\d{2}:\\d{2})</td>"
Btw.^2 You shouldn't use regex to parse HTML. Use an HTML parser instead. There are plenty around. If you try to parse HTML with regex you are soon coming to major problems (nesting, wrong HTML, like missing end tags etc.).

Check if a string ends in is valid or not in java using regex

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.

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

Categories