regex for matching and excluding the rest [duplicate] - regex

This question already has answers here:
Regex match entire words only
(7 answers)
Closed 6 years ago.
i want to match a very simple number-bar-number pattern : 1/2
My regex is: ([0-9]{1}\/[0-9]{1})
The problem is that I match things I want to exclude. I need exact matching excluding the rest.
My regex return as valid patterns as :
1/12344
2/23ABC
2/233423/2425
[update]
tested with some txt files using GREP, still having issues. By instance:
2/3/16 (it's a date and it matches the pattern, so grep returned the entire line)
I'm not very versed on regex so any help would be very much appreciated
Regards

Try this
(?:^|\s)(\d+\/\d+)(?=\s|$)
Regex demo
Explanation:
(?: … ): Non-capturing group sample
^: Start of string or start of line depending on multiline mode sample
|: Alternation / OR operand sample
\: Escapes a special character sample
( … ): Capturing group sample
+: One or more sample
(?=…): Positive lookahead sample
$: End of string or end of line depending on multiline mode sample

Related

Regex for excluding files with specific pattern [duplicate]

This question already has answers here:
Regex: match everything but a specific pattern
(6 answers)
Closed 2 years ago.
Hey I have a list of files
B123245.xml
B123245-ext.xml
1234W01.xml
1234W01-ext.xml
Now I need a regular expression filter only the files without -ext in the name.
I tried already this ^.+(?!-ext)\.xml$
but it is not working.
What am I doing wrong?
Not sure about your exact needs, but if you want to exclude those file where "-ext" is right before the xml extension I think you could use:
^.+(?<!-ext)\.xml$
See the demo
^ - Start string anchor.
.+ - 1+ character apart from newline.
(?<!-ext) - A negative lookbehind to assert position isn't preceded by "-ext".
\.xml - Match a literal dot and "xml".
$ - End string anchor.
With the help of user 'The fourth bird' I found out the correct structure.
Here is the correct result
^(?!.*-ext).+\.xml$

Regexp to match multi-line string [duplicate]

This question already has answers here:
What is a non-capturing group in regular expressions?
(18 answers)
Closed 2 years ago.
I have this regexp:
^(?<FOOTER_TYPE>[ a-zA-Z0-9-]+)?(?<SEPARATOR>:)?(?<FOOTER>(?<=:)(.|[\r\n](?![\r\n]))*)?
Which I'm using to match text like:
BREAKING CHANGE: test
my multiline
string.
This is not matched
You can see the result here https://regex101.com/r/gGroPK/1
However, why is there the last Group 4 ?
You will need to make last group non-capturing:
^(?<FOOTER_TYPE>[ a-zA-Z0-9-]+)?(?<SEPARATOR>:)?(?<FOOTER>(?<=:)(?:.|[\r\n](?![\r\n]))*)?
Make note of:
(?:.|[\r\n](?![\r\n]))*)?
(?: at the start makes this optional group non-capturing.
Updated Demo
it is group 4 because the fourth parentheses you defined is:
(.|[\r\n](?![\r\n]))*)
it translate to
"either dot, or the following regex"
and in the example you have, it ends on a dot.
string.
so as regex is usually greedy, it captures dot as the forth group

Find DATE match starting from end of string [duplicate]

This question already has answers here:
Regex Last occurrence?
(7 answers)
Closed 3 years ago.
I have the following RegEx syntax that will match the first date found.
([0-9]+)/([0-9]+)/([0-9]+)
However, I would like to start from the end of the content and search backwards. In other words, in the below example, my syntax will always match the first date, but I want it to match the last instead.
Some Text here
01/02/15
Some additional
text here.
10/04/14
Ending text
here
I believe this is possible by using a negative lookahead, but all my attempts failed at this because I don't understand RegEx enough. Help would be appreciated.
Note: my application uses RegEx PCRP.
You could make the dot match a newline using for example an inline modifier (?s) and match until the end of the string.
Then make use of backtracking until the last occurrence of the date like pattern and precede the first digit with a word boundary.
Use \K to forget what was matched and match the date like pattern.
^(?s).*\b\K[0-9]+/[0-9]+/[0-9]+
Regex demo
Note that the pattern is a very broad match and does not validate a date itself.

How to negate angular pattern/regex [duplicate]

This question already has answers here:
regular expression for anything but an empty string
(9 answers)
Closed 4 years ago.
I have the following code
https://stackblitz.com/edit/angular-uvxifq-qrjtpg
pattern="^\s+$"
I need to negate this, because as of now I got error when I put a letter, but it should only show error IF there is only white space.
I tried using ?! but it shows an error
You can use the following regex:
^(?: *[^\s] *)+$
demo: https://regex101.com/r/th0A3A/3/
If you want to use a negative lookahead (?!, you could check if from the start till the end of the string there are no whitespace characters:
pattern="^(?!\s*$)[\s\S]+$"
Regex demo
That will match
^ Start of string
(?!\s*$) Negative lookahead, assert that what follows is not 0+ whitespace characters and end of string
[\s\S]+ Match 1+ times any character including new lines
$ End of string

Regex for string containing one string, but not another [duplicate]

This question already has answers here:
Regular expression for a string containing one word but not another
(5 answers)
Closed 3 years ago.
Have regex in our project that matches any url that contains the string
"/pdf/":
(.+)/pdf/.+
Need to modify it so that it won't match urls that also contain "help"
Example:
Shouldn't match: "/dealer/help/us/en/pdf/simple.pdf"
Should match: "/dealer/us/en/pdf/simple.pdf"
If lookarounds are supported, this is very easy to achieve:
(?=.*/pdf/)(?!.*help)(.+)
See a demo on regex101.com.
(?:^|\s)((?:[^h ]|h(?!elp))+\/pdf\/\S*)(?:$|\s)
First thing is match either a space or the start of a line
(?:^|\s)
Then we match anything that is not a or h OR any h that does not have elp behind it, one or more times +, until we find a /pdf/, then match non-space characters \S any number of times *.
((?:[^h ]|h(?!elp))+\/pdf\/\S*)
If we want to detect help after the /pdf/, we can duplicate matching from the start.
((?:[^h ]|h(?!elp))+\/pdf\/(?:[^h ]|h(?!elp))+)
Finally, we match a or end line/string ($)
(?:$|\s)
The full match will include leading/trailing spaces, and should be stripped. If you use capture group 1, you don't need to strip the ends.
Example on regex101