I would like to match a String with a look-ahead using the following regex: /A20.(?!4)/. This string should match:
A20.1
A20.2
A20.3
A20.41
A20.42
A20.400
...
The only A20* string that should not match is
A20.4
It works fine, expect A20.41 or A20.42.. How can I terminate the regex?
I have tried /A20.(?!4)$/, but it did not work.
You could use negated character class such as [^4], which would mean "match everything except four". But I think you still want to match only digits, so I'd simply use character class [123567890] for that (note that 4 is excluded).
So pattern would be:
A20\.[123567890]
Also, you use . (dot) to match the dot, but dot is special regex character, so you need to escape it to treat it literally: \.
you have to look ahead more. If there is another digit behind the 4 then match it, so:
A20.((?!4)|(?=\d\d))
Related
I am trying to match a word with regex. for example, I want to match only first 2 folders in below string
/folder1/folder2/filder3/folder4/folder5
I wrote a below regex to match first two folders but it matches everything till /folder5 but I wanted to match only till /folder2
/(\w.+){2}
I guess .+ matches everything. Any idea how to handle this?
You can use
^/[^/]+/[^/]+
^(?:/[^/]+){2}
Or, if you need to escape slashes:
^\/[^\/]+\/[^\/]+
^(?:\/[^\/]+){2}
See the regex demo. [^/] is a negated character class that matches any char other than a / char.
I have an Ordernumber that looks like this: 1001-00001.2 but I want it without the extra .2
I tried to use the following in my code:
{$sArticle.ordernumber|regex_replace:"/'.'/\d":" "}
it didn't work because I don't know how I can use the dot.
Dot matches any single character. Try escaping it:
{$sArticle.ordernumber|regex_replace:"/\.\d+$/":""}
There's actually a few changes, here.
Everything was moved to be within the /.../ that mark a regex
The dot was escaped with \.
A quantifier (+) was added to \d so it matches one or more digits.
An anchor ($) is used to make sure it doesn't match anywhere but the end of the string.
The replacement is now an empty string
Which regex allows me to match characters and digits from String GIVEN_CHAR_VAL":"AKRONIS387226279863_NXUS0000000016092126"
I tried
GIVEN_CHAR_VAL":"(.*)"
but doesn't work correct.
Any ideas?
If you only want to match alphanumeric characters, use \w rather than .:
GIVEN_CHAR_VAL":"(\w*)"
Your suggested regex works for me, but have you tried:
GIVEN_CHAR_VAL":"(.*?)"
What do you actually want to match?
.* will give you the entire set AKRONIS387226279863_NXUS0000000016092126
\w+ as suggested above will do the same because it accepts '_'
If you are trying to match everything except the underscore try something more specific like [A-Z0-9]+ though you will end up with two matches because of the intervening underscore.
I have this regular expression
([A-Z], )*
which should match something like
test, (with a space after the comma)
How to I change the regex expression so that if there are any characters after the space then it doesn't match.
For example if I had:
test, test
I'm looking to do something similar to
([A-Z], ~[A-Z])*
Cheers
Use the following regular expression:
^[A-Za-z]*, $
Explanation:
^ matches the start of the string.
[A-Za-z]* matches 0 or more letters (case-insensitive) -- replace * with + to require 1 or more letters.
, matches a comma followed by a space.
$ matches the end of the string, so if there's anything after the comma and space then the match will fail.
As has been mentioned, you should specify which language you're using when you ask a Regex question, since there are many different varieties that have their own idiosyncrasies.
^([A-Z]+, )?$
The difference between mine and Donut is that he will match , and fail for the empty string, mine will match the empty string and fail for ,. (and that his is more case-insensitive than mine. With mine you'll have to add case-insensitivity to the options of your regex function, but it's like your example)
I am not sure which regex engine/language you are using, but there is often something like a negative character groups [^a-z] meaning "everything other than a character".
How can I match all characters including new line with a regex.
I am trying to match all characters between brackets "()". I don't want to activate Dot matches all.
I tried
\([.\n\r]*\)
But it doesn't work.
(.*\) This doesn't work if there is an new line between the brackets.
I have been using http://regexpal.com/ to test my regular expressions. Tell me if you know something better.
I'd usually use something like \([\S\s]*\) in this situation.
The [\S\s] will match any whitespace or non-whitespace character.
The first example doesn't work because inside a character class the dot is treated literally (Matches the . character instead of all characters).
\((.|[\n\r])*\)