Regular Expression : Multiline check problem - java

Hello i have problem with this regexp
!
interface TenGigabitEthernet 1/49
description Uplink
no ip address
switchport
no shutdown
!
interface TenGigabitEthernet 1/50
no ip address
shutdown
!
interface TenGigabitEthernet 1/51
no ip address
shutdown
!
i tried this regexp (interface) ((.\s.)+) but it is not working becuse it match "interface" and the rest of text
I need to catch in first group "interface" and in the second i need all until first occur of "!"
so for example:
first group:
interface
second group:
TenGigabitEthernet 1/51
no ip address
shutdown
How i can do this?

Try this:
(interface)\s+([^!]+)
Here Is Demo

Use this:
(interface)\s*([^!]+) /g
The first group captures the hard-coded interface. The second group captures everything other than !, by skipping the leading whitespaces, if any. The global flag /g ensures all matches.
Demo

If the content itself can contain a !, you could check for a ! at the start of the line and repeat matching all lines until you encounter a ! at the start.
^(interface)\s*(.*(?:\n(?!!).*)*)
In Java
String regex = "^(interface)\\s*(.*(?:\\n(?!!).*)*)";
Regex demo

Related

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

Regex exclude lines not containing words but some words

I am using jenkins editable email plugin where i need to use regex to filter logs
I have this regex
regex="(.*)fatal:(.*)"
which basically matches line start with fatal: so that i can grab errors
Example line look like this
fatal: [localhost]: FAILED! => {"attempts": 1, "changed": true, "cmd": "nslookup test1.local",
But i want to grab all fatal erros except which conatin few worda like nslookup.
I want to ignore the above line which has nslookup and all other should be ok
You can do a negative look ahead with a regex. This would work for your example:
^(.*)fatal: ((?!nsookup).)*$
To check for two different lookahead words:
^(.*)fatal: ((?!nslookup)(?!stuff).)*$
The (.*) at the beginning is only required if there can be something (whitespace or other characters) before "fatal:"
You can play with the expression with this link: https://regex101.com/r/ezxA5s/1

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

Extract certain words from predefined sentence using regular expression

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.

Categories