regex match string but not another - c#

I have the two following strings
/signon/default.wl?rs=WLW11.10&vr=2.0&fn=_top
/signon/default.wl?fn=%5Ftop&newdoor=true&rs=WLW11%2E10&vr=2%2
I would like to match all the strings except the ones that do not contain newdoor
so far i have the following regex
/signon/default.wl\?(?=[\w]*)(?!newdoor)
but it matches all strings.
can anyone point out what im doing wrong.

You can try this /signon/default.wl\?(?!.*newdoor).*
It asserts using negative lookahead that there is no occurrence of newdoor in the input string. Code will look like this
resultString = Regex.Match(subjectString, #"/signon/default.wl\?(?!.*newdoor).*",
RegexOptions.IgnoreCase).Value;

/signon/default.wl\?(?=[\w]*)(?!newdoor)
Both (?=) and (?!) are zero-width assertions that don't consume any input. Try:
/signon/default.wl\?(.(?!newdoor))*$

why not use something like:
subjectString.contains("newdoor");

Related

Regex start with exactly two alphabetic characters and than followed by any digits

I'm trying to create a very simple regex that accepts strings:
starts with two [A-Za-z] characters
ends with any number of digits
my code is:
Regex.IsMatch("AA00000000000", $"^[A-Za-z]{2}[0-9]*$")
but it returns False. Where am I wrong? I've already tested the same regex with the same input on regexstorm.net and it works.
Working for me with below code:
string pattern = #"^[A-Za-z]{2}[0-9]*$";
string str = "AA00000000000";
bool val = Regex.IsMatch(str, pattern);
You put a "$" in front of your regular expression, because of that the "{2}" part will be interpreted as just "2", so the regex that gets evaluated looks like this: "^[A-Za-z]2[0-9]*$".
Regex.IsMatch method return boolean value. Please, try with this way. You can also try with using "$"

Regular Expression for a JSON-type String

I am having trouble splitting a String using a regular expression
"[{'name':'abc','surname':'def'},{'name':'ghi','surname':'jkl'},{'name':'asdf','surname':'asdf'}]"
Now I'd like to split this to
"{'name':'abc','surname':'def'}" and "{'name':'ghi','surname':'jkl'}"
Later on I will deserialize both Strings and work with the values. I must admit that I've worked way too little with regular expressions and would love if someone could help me. I want to split by those square brackets as well as by the middle comma. I was either splitting by ALL commas or not splitting at all.
Kind regards
This Regex will do that:
({.*?})
and here is a Regex 101 to prove it.
To use it you might do something like this:
var match = Regex.Match(input, pattern);
// match.Groups has all of the matches

Pattern not surrounded by numbers or alphanumerics

I would like to find some words not surrounded by any numerics or alphanumerics or surrounded by nothing.
So if I'm looking for Foo123 I would like this result
="Foo123"; =>TRUE
barFoo123; =>FALSE
Foo123 =>TRUE
BarBar123Foo123Bar; =>FALSE
;Foo123 =>TRUE
I just built this expression:
(^[^0-9a-zA-Z]?)WORDTOFIND([^0-9a-zA-Z]?$)
I was pretty sure I'm in the right way but when I'm using it like this:
if (Regex.IsMatch(line, string.Format(#"(^[^0-9a-zA-Z]?){0}([^0-9a-zA-Z]?$)",snCode)) )
{
}
It doesn't work. What am I doing wrong?
You're essentially looking for
(?<![a-zA-Z0-9])Foo123(?![a-zA-Z0-9])
This uses a lookahead and lookbehind to make sure that there isn't an alphanumeric character either before or after Foo123. This assumes ASCII, though.
The following matches the strings you do NOT want so do !Regex.IsMatch()
(^([0-9a-zA-Z])+{0}.*)|(.*{0}([0-9a-zA-Z])+)

How to ignore words in string using Regular Expressions

I need a piece of regex that can be used to do a NOT match. Take for example the following URL's
http://www.site.com/layout/default.aspx
http://www.site.com/default.aspx
http://www.site.com/layout.aspx
The regex should NOT match any url string that contains the directory "layout"
http://www.site.com/layout/default.aspx
and instead should match on
http://www.site.com/default.aspx
http://www.site.com/layout.aspx
How can i do this using .NET regex?
Use negative lookahead:
^(?!.*/layout/)
You have to anchor to the start of the string or you'll get false positives i.e. (?!/layout/) alone won't work.
If you need to squeeze everything in one regex, try negative lookahead, something along the lines of this:
(?!layout)
Just match /layout/ and invert the result, whatever language you use.
E.g. with PHP:
if(!preg_match('#/layout/#i', $url)) {
// does not match layout
}
PYTHON:
import re
if not re.match('layout'):
#do whatever here
re is regex for python

What is the regex for *abc?

I am trying to use Regex to find out if a string matches *abc - in other words, it starts with anything but finishes with "abc"?
What is the regex expression for this?
I tried *abc but "Regex.Matches" returns true for xxabcd, which is not what I want.
abc$
You need the $ to match the end of the string.
.*abc$
should do.
So you have a few "fish" here, but here's how to fish.
An online expression library and .NET-based tester: RegEx Library
An online Ruby-based tester (faster than the .NET one) Rubular
A windows app for testing exressions (most fully-featured, but no zero-width look-aheads or behind) RegEx Coach
Try this instead:
.*abc$
The $ matches the end of the line.
^.*abc$
Will capture any line ending in abc.
It depends on what exactly you're looking for. If you're trying to match whole lines, like:
a line with words and spacesabc
you could do:
^.*abc$
Where ^ matches the beginning of a line and $ the end.
But if you're matching words in a line, e.g.
trying to match thisabc and thisabc but not thisabcd
You will have to do something like:
\w*abc(?!\w)
This means, match any number of continuous characters, followed by abc and then anything but a character (e.g. whitespace or the end of the line).
If you want a string of 4 characters ending in abc use, /^.abc$/

Categories

Resources