Regex to match this - regex

I'm trying to capture from PHPUnit output the file path and the error line with a condition.
I don't want want lines that contain the whole word exception (can I exclude multiple words?).
This is my output and non-working (obviously:) pattern:
/path/includes/exception.php:7
/path/things-i-care-about/es/somefile.php:132
/path/things-i-care-about/es/somefile.php:121
/path/things-i-care-about/es/somefile.php:54
/path/things-i-care-about/es/somefile.php:60
/path/things-i-care-about/es/somefile.php:41
/path/things-i-care-about/es/somefile.php:47
/path/things-i-care-about/testfile.php:26
Pattern: /((?!exception).*.php):(\d.*)/gs
What I tried is negating any line that has "exception" in it, but my regex didn't quite work.
What am I doing wrong?

You can try this pattern:
^(?:[^e\n]+|\Be|\be(?!xception\b))+\.php:\d+$
or this pattern, if you don't need to check a specific line format:
^(?>[^e\n]++|\Be|\be(?!xception\b))+$
Notice: If you need to select all consecutive lines in one block, you just need to remove \n from the character classes.

Related

Remove texts based on pattern

I have a file with lots of URLs:
domain1.com/blue
domain1.com/blue/
domain2.com/red
domain2.com/red/
...
[etc]
Is there a way for me to use Regex formula to keep ONLY the "domain1.com/blue" type of text, but DELETE "domain1.com/blue/"?
The pattern is that all these URLs' end have the first part the same, but at the end some have a "/"; basically i want to remove all the URLs that have the "/" at the end but keep the ones without "/" at the end.
In the end the the file should only contain these:
domain1.com/blue
domain2.com/red
...
[etc]
Thank you so much for the help! If anyone has an idea how to do this, it'd be awesome!
There are two things that you could do that I can think of.
1, Match all the lines in the file that do not satisfy your pattern and replace it with a single new line.
The regex for this /^.*\/$/ and then replace with whatever.
2, Match only the lines you want to keep and save them to a new file.
The regex /^((?!(\/$)).)*$/
you can paste these into a regex translator for in depth explanation as to what they're doing
Unfortunately, you did not specify which language you are using with regex, so I can't give you language-specific details. But a line that ends with / followed by possibly one or more white space characters can be tested for with the following regular expression:
/\/\s*$/
So read in each line and test it against the above regex. It it matches, do not write it out to the new file.
See Regex Demo

regex to exclude string unless another string exists

I'm currently working on a fluentd regex expression to match all log entries unless the message contains the string "dbug". However, if the log contains both "dbug" and "firing" in the entry then I need the regex to match that string.
The two strings can appear anywhere in the log entry however "dbug" will always be before "firing".
Is it possible to build a single regex expression that can do this?
I'd appreciate any help on this!
We can try using lookaheads here, e.g.
^(?:(?!.*\bdbug\b)|(?=.*\bdbug\b.*\bfiring\b)).*$
Demo
The first lookahead (?!.*\bdbug\b) matches any line which does not contain dbug at all, and the second lookahead (?=.*\bdbug\b.*\bfiring\b) matches any line which contains both dbug and firing, in that order.

Regex Match Paragraph Pattern

I am trying to match a paragraph pattern and I am having trouble.
The pattern is:
[image.gif]
some words, usually a few lines
name
emailaddress<mailto:theemailaddress#mail.com>
I tried matching everything between the gif image and the <mailto: but this happens multiple times in the file meaning I get a bad result.
I tried it with this
(?<=\[image.gif\].*?(\[image.gif\])).*?(?=<mailto:)
Is there a way to use Regex to match the general layout of a paragraph?
"the general layout of a paragraph" needs a better definition. Given the lack of an input plus expected output, I'm having to guess what you want here. I'm also guessing that you will accept any language. Here's perl, almost certainly not a language you're familiar with.
Assumed input:
do not match this line
[image.gif]
some words, usually a few lines
Bobert McBobson
emailaddress<mailto:bobertmb#example.com>
don't match this line either
[image.gif]
another few words
on another few lines
Bobina Robertsdaughter
emailaddress<mailto:bobinard#example.info>
this line is also not for matching
Expected output:
[image.gif]
some words, usually a few lines
Bobert McBobson
emailaddress<mailto:bobertmb#example.com>
---
[image.gif]
another few words
on another few lines
Bobina Robertsdaughter
emailaddress<mailto:bobinard#example.info>
Solution using perl:
#!/usr/bin/perl -n007
my $sep = "";
while (/(\[image\.gif\].*?<mailto:[^>]*>(\r)?\n)/gms) {
print $sep . $1;
$sep = "---$2\n";
}
perl is the king of regex languages; many would say that's all it is good for. Here, we use the -n007 option to tell it to read the entire contents of each file and run the code on it as the default variable.
$sep starts blank because there's nothing to separate until the second match.
Then we loop over each block of text that matches the regex:
matches a literal [image.gif]
then matches as little content following that as possible
then matches a literal <mailto: and continues until the next >
then captures the line break (including optional support for DOS line endings)
(see full regex explanation and example at regex101)
We then print the match and finally set the separator to three dashes and a line break (DOS line endings added when needed).
Now you can run it:
$ perl answer.pl input.txt
[image.gif]
some words, usually a few lines
Bobert McBobson
emailaddress<mailto:bobertmb#example.com>
---
[image.gif]
another few words
on another few lines
Bobina Robertsdaughter
emailaddress<mailto:bobinard#example.info>

How to match all lines with common pattern in splunk regex

I am trying to extract a report of all incidents matching a certain pattern and then need to plot how many occurances of each type. For example the below lines.
File: ../../../transfer/200.FILETYPE1.0000003115.20160419-082708-089.xml successfully imported.
some other logs....
File: ../../../transfer/200.FILETYPE1.0000003116.20160419-082708-090.xml successfully imported.
some other logs...
File: ../../../transfer/201.FILETYPE2.0000003117.20160419-082708-091.xml successfully imported.
Please note that there are many filetypes but the pattern is same "/transfer/" prefix and "successfully imported." suffix and these prefix and suffix must match as other lines may also contain same file name before completion.
So in above case I need to find all such occurrences of above lines and find count of each FILETYPE1 and FILETYPE2 in splunk.
Can someone help me with regex that can match above pattern and give me all such lines so that I can extract counts of each file type?
Straight forward:
^File:.*FILETYPE\d.*$
# ^ beginning of the line
# File: literally
# .* anything to the end of the line
# FILETYPE + a number literally
# .* anything afterwards
# $ the end of the line
See a demo on regex101.com.
Hint: If you only have these two strings (FILETYPE1 and FILETYPE2) you might be faster with string functions only.
Edit FILETYPE1/FILETYPE2 for counting
\.\.\/.*\/\d+\.FILETYPE1\..*?\.xml
Regex demo
Try this one:
\/transfer\/.*FILETYPE(\d+).*successfully imported
The file type number will be captured by the capture group, so you can count the file occurrences
Regex Demo

Regex to get matching line from a text file

I have the following text in a text file.
SREV_OpportunityHandler.OppBeforeUpdate(Trigger.New, Trigger.OldMap);
SREV_OpportunityHandler.OppAfterUpdate(Trigger.New, Trigger.OldMap);
The line which I want will always start with SREV_<SomeChar> Handler.<MethodName>()
I want to get the just the method name by regEx. There may be N number of such lines, so in case of above text I need to match the following 2 strings:
OppBeforeUpdate
OppAfterUpdate
The first capturing group will hold the required method name and should work with all NFA regex engines:
\bSREV\w+Handler\.(\w+)(?=\()