Regex for last point or comma in string - regex

I'm looking for a regex with gives me the last occurance of a point or a comma in a string (whichever one is the further back)
From what I googled myself I got this one ([,\.])(?!.*\1) but this gives me two results per string which isnt what I wanted.
I hope somebody can help me as I'm struggeling for the correct keyword to google for.
Cheers and thank you very much in advance

This pattern ([,\.])(?!.*\1) matches the last comma or dot asserting not any of the 2 being present on the right anymore.
That can occur for both a dot and a comma so you could possibly get 2 matches.
If you want a single match, you can match one of them and assert no more occurrences of either one of them to the right using a negated character class [^,.\r\n]* matching any char except the listed characters.
Note that you don't need the capture group for a match only.
[,.](?=[^,.\r\n]*$)
See a regex demo.

In this regex pattern the first capturing group is the last comma or point in each line.
We achieve this by looking for a comma or point and not allowing a point, comma or line ending between it and the end of the line.
([.,])[^.,\n\r]*$

Related

Match last digit of string

I haven't found what I am looking for, but I am trying to match the last digit of strings. For example, I want to pull out only those that end in the number 9 from the data below. How can this be done in regex? I'm just beginning to play around with it, so any help is appreciated. Thanks!
abc12997654
efg12456789
dkj98765433
kij12444449
yur123ert97
output:
efg12456789
kij12444449
One possible way to do that (of course there are alternatives) is as follows:
It matches the words only ending with a digit 9.
/(\w+9\b)/g
(.*9$) is a simpler regex that will give you your output!
Regex Demo
you can use word boundaries to assert a correct match occurs.
regex: \b\w+9\b
https://regex101.com/r/hAA0pC/1
\b\w+9\b
\b matches a non word character, in this case start and end of the string.
\w matches a alpha-numeric or _ character, + grabs one or more matching characters.

Regex check for name Initials

I am trying to create a regex that checks if one or more middle-name initials have the following stucture:
INITIAL.[BLANK]INITIAL.[BLANK]INITIAL.
There can be multiple Initials as long as they are followed by a dot (.) - blank spaces are only allowed between two initials (e.g. L. B.)
It should not be possible to have a space after an initial if there's no other initial following.
At the moment, I have the following Regex which doesn't work perfectly as of now:
([A-Z]\. (?=[A-Z]|$))+
Using regex101, this is an example:
As you can see, it still matches the string even though there's a blank space at the end, without having another Initial following.
I am not sure why this is happening. I am just learning regex and would be glad if anyone could provide me with a solution to my problem :)
The error you're seeing is because at the last step, your expression reads in [A-Z]\. looks ahead for $ (and finds it). I would express the pattern this way: (?:[A-Z]\. )*[A-Z]\.$. Treat the last initial specially because it does not have a final space.
The pattern you tried ([A-Z]\. (?=[A-Z]|$))+ uses a repeated capturing group which will give you the value of the last iteration.
In that repetition you match a space <code>[A-Z]\. </code> effectively meaning that it should be present in the match.
You could repeat 0+ times matching a char [A-Z] followed by a space to match multiple occurrences.
Then match a char [A-Z] asserting what is on the right is not a non whitespace char.
\b(?:[A-Z]\. )*[A-Z]\.(?!\S)
Regex demo
If there can be multiple spaces but it should not match a newline:
\b(?:[A-Z]\.[^\S\r\n]*)*[A-Z]\.(?!\S)
Regex demo

Regex String Within Negative Lookahead being selected

So i am trying to write something that will select the colon and number in this situation... ie ":1"
"phoneNumber":1111111111
but not in a situation where the colon followed by a digit is between a pair of quotes... ie not match ':0' and ':2'
"lastLogon":"2019-04-17 14:08:25.732576"
I have this expression which selects everything in quote pairs.
((?=["]).+?(?=["])")
Which I tried to do the following with...
:\s?([-\d])(?!((?=["]).+?(?=["])"))
But this selects both of the occurrences above. Does anyone have a workaround, I think I might be misunderstanding how negative look-ahead works.
Thanks!
Edit:
Added info on what strings I wanted to match.
Just match a colon followed by 3 or more digits:
:\d{3,}

Extract Names from Field (Regex)

I'm trying to extract First and Last name from a string that looks like this:
CN=First\, Last,OU=Standard users,OU=Users,OU=Place,OU=DOMAIN,DC=dfe,DC=stuff,DC=asdf
([^CN=,\\])([a-zA-Z]*)?(?!OU)
My attempt is above, but it obviously doesn't work.
Can anyone point me in the correct direction?
Thanks
You can use this expression:
^CN=(.*?)\\, (.*?),
Live demo. It uses two capturing groups for first and last names with the other static text around them.
CN=(.*)\\, ([^,]*),
This should get your First until the \, and last until the next comma.
You could come up with:
CN=(?P[^,]+),\s*
(?P[^,]+)
See a demo on regex101.com.
In the regex you have tried, the [^CN=,\\] this part will do exactly the reverse of what you need, like will match the characters except CN=,\\ these.
You can use:
^CN=[^,]+,\s+[^,]+
^CN=(.+)\\, (.+?),
This will get you First in the first capture group and Last in the second capture group, assuming everything matches the pattern of line starting with CN=, then first name, then \,, then last name, followed by ,. First and last are will not be limited to only letters though.
When you put the ^ inside of brackets like you attempted, [^CN=,\\], then you are telling Regex to look for any characters except C, N, =, ,, and \.

How to match text which the part of it is already matched previous?

I have a string like aaa**b***c****ddd, and I want to get a sequence of matched text of pattern [^*]\*+[^*], which should I thank be [a**b, b***c, c***d]. However, when I test this in text editor like vim or emacs, the second (b***c) is not matched.
aaa**b***c***ddd
|--| |---|
first third
|---|
second, which I think should be matched but not
How should I modify the regular expression to match the second?
Yes you can, the trick consists to put all in a capturing group inside a lookahead to allow overlapping results:
(?=([^*]\*+[^*]))
But you can't use this do to replacements since this pattern matches nothing. (or perhaps if you can get the capture group length and the current offset)
EDIT:
it seems to be possible to obtain the capture group length with vim with strlen(submatch(1))
#CommuSoft is correct. One way to approach this problem would be to match the whole string against this regex and then the second time around, you match this regex against the substring that starts at (index_of_first_previous_match + 1) until the end of the string. Hope that is clear.
So if the index of your first match above (a**b) was 2. Then the new substring that you match against the regex the second time should start from index 3 till the end of the string. This will give you the two results.
However, Casimir's answer is much simpler.