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,}
Related
I'm trying to find a regular expression to find [number2] in [number],[number2][word].
So far I've tried with [,](\d*), but it also gets me the comma.
Demo: https://regexr.com/59eqa
You may use:
(?<=,)(\d*)
Regex Demo
Detail:
(?<=,): positive look behind that doesn't consume character but indicate that the number must have , before it
The previous answers do not handle the case that the second (or two numbers) is matched.
If the second number must be captured, this can be done with
\b\d+,(\d+)[A-Za-z]
where the "number2" is contained in captured group 1.
If you want to get the match only, you could use 2 lookarounds, asserting a comma to the left and a char a-zA-Z to the right.
Use \d+ to match 1 or more digits.
(?<=,)\d+(?=[a-zA-Z])
Regex demo
If there should be a digit before the comma as well:
(?<=\d,)\d+(?=[a-zA-Z])
Regex demo
I would like to search for the literal US followed by a digit, that is repeated one or more times, followed by anything except a dash. For example, these should match:
US3.
US22?
US134!
while these don't
US5-
US66-
US789-
I have tried
r'US[0-9]+(?=[^-])'
but it also matches
'US6', 'US78'
How do I modify this?
Mention the list of characters after matching digits in a character class.
Regex: US\d+[?!&%.]
Regex101 Demo
I'm not sure if this is the absolute best way to go about doing this, but if you know that there is only one of these each line, you could just add a $ to the positive lookahead like so:
US[0-9]+(?=[^-]$)
Regexr.com example
You have to mention the list of characters in character class that are allowed.
I have attached the screenshot of the output , you can verify it !
US[0-9]+[?!&%.]
Say the string is ‘abc’ and the expression is (?=a)abc, would that not be the same as just searching for abc? When do positive lookarounds have purpose over not using them?
Positive lookahead works just the same. q(?=u) matches a q that is followed by a u, without making the u part of the match. The positive lookahead construct is a pair of parentheses, with the opening parenthesis followed by a question mark and an equals sign.
http://www.regular-expressions.info/lookaround.html
Here is a small example from https://ourcraft.wordpress.com/2009/03/25/positive-examples-of-positive-and-negative-lookahead/
Say I want to retrieve from a text document all the words that are immediately followed by a comma. We’ll use this example string:
What then, said I, shall I do? You shan't, he replied, do anything.
As a first attempt, I could use this regular expression to get one or more word parts followed by a comma:
[A-Za-z']+,
This yields four results over the string:
then,
I,
shan't,
replied,
Notice that this gets me the comma too, though, which I would then have to remove. Wouldn’t it be better if we could express that we want to match a word that is followed by a comma without also matching the comma?
We can do that by modifying our regex as follows:
[A-Za-z']+(?=,)
This matches groups of word characters that are followed by a comma, but because of the use of lookahead the comma is not part of the matched text (just as we want it not to be). The modified regex results in these matches:
then
I
shan't
replied
For this example hello.1.2.3.4.world I want to match a result which gives me 1.2.3.4. Number of digits between dots doesn't matter. As long as it follow digit.digit pattern
My part solution was following regular-expression [\d.]+.[^.a-z], which gives me .1.2.3.4 as result. And I strip the first dot by using trim or similar method.
Any regexp master who can tell me how to rid the first dot with one regular expression only?
How about this: \.(\d(?:\.\d)*)\.\D
EDIT:
(\d+(?:\.\d+)*)
Demo
If you want to use your current regex you can put a lookahead at the start, and escape the literal dot when not inside a character group (?=\d)[\d.]+\.[^.a-z]
The lookahead (?=\d) will make sure the first character matched is a digit.
Demo here
:Statement
Say we have following three records, and we just want to match the first one only -- exactly one digital followed by a specific word, what is the regular expression can be used to make it(in NotePad ++)?
2Cups
11Cups
222Cups
The expressions I tried and their problems are:
Proposal 1:\d{1}Cups
it will find the "1Cups" and "2Cups" substrings in the second and third record respectively, which is what we do not want here
Proposal 2:[^0-9]+[0-9]Cups
same as the above
(PS: the records can be "XX 2Cups", "YY22Cups" and "XYZ 333Cups", i.e., no assumption on the position of the matchable parts)
Any suggestions?
:Reference
[1] The reg definition in NotePad++ (Same as SciTe)
As mentioned in Searching for a complex Regular Expression to use with Notepad++, it is: http://www.scintilla.org/SciTERegEx.html
[2] Matching exact number of digits
Here is an example: regular expression to match exactly 5 digits.
However, we do not want to find the match-able substring in longer records here.
If the string actually has the numbered sequence (1. 2Cups 2. 11Cups), you can use the white space that follows it:
\s\d{1}Cups
If there isn't the numbered list before, but the string will be at the beginning of the line, you can anchor it there:
^\d{1}Cups
Tested in Notepad++ v6.5.1 (Unicode).
It sounds like you want to match the digit only at the start of the string or if it has a space before it, so this would work:
(^|\b)\dCups
Debuggex Demo
Explanation:
(^|\b) Match the start of the string or beginning of a word (technically, word break)
\d Match a digit ({1} is redundant)
Cups Match Cups
This will work:
\b\dCups
If "Cups" must be a whole word (ie not matching 2Cupsizes:
\b\dCups\b
Note that \b matches even if at start or end of input.
I found one possible solution:
Using ^\d{1}Cups to match "Starting with one digital + Cups" cases, as suggested by Ken, Cottrell and Bohemian.
Using [^\d]\dCups to match other cases.
However, haven't found a solution using just one regex to solve the problem yet.
Have a try with:
(?:^|\D)\dCups
This will match xCups only if there aren't digit before.