I am looking for a regex which can match the following conditions.
It always starts with "someId":[ and ends with ].
It must contain the number 25 within the square brackets.
There may be numbers before and after number 25
The numbers are separated with a comma (,) apart from the last number
For example:
"someId":[25]
"someId":[25,27]
"someId":[1,4,25]
"someId":[1,4,25,27,30]
I have the following regex which works, however I was wondering if theres a better way to do it which isn't as greedy.
"someId":\[(\d{1,2},)*?25,?(\d{1,2},)*?(\d{1,2})?]
a bit simplified:
"someId":\[(\d+,)*25(,\d+)*\]
Related
I am trying to clean up a field so that it only has integers or floating numbers.
Basically, I want to the row to be blank if there are dates or text.
This catches most things:
regex_replace("^(\d*.\d*).*","$1")
but leaves the initial numbers if the row is a date (i.e. 2022 if 2022-07-01 20:30:29 or 7 if 7/1/2022).
0
0
1
15
1.8910127482598
2022-07-01 20:30:29
7/1/2022
West
Living
C000000475
1
0
0
0
How can I modify the regex so that it removes the dates as well?
TIA,
LCH
Find all numbers
^(\d*\.?\d*)$
Your regular expression uses \d.\d which is probably not intended by you. The . must be escaped, otherwise it will be interpreted as "any character".
Notice I wrote \.? to find an optional decimal point. A . means "any character", not the decimal dot. We therefore escape it.
I added the $ at the end to denote "end of line".
Replacing with $1 just leaves the number. Use an empty string to remove numbers.
Find a playground on regex101 here:
https://regex101.com/r/P7jwNV/1
This is a slightly tweaked version of your expression. However, it will go through the lines and replace the number with themselves. How would that leave the other rows empty?
Remove the numbers
You say you want to remove the non-numbers, however your regular expression is trying to find numbers and replace them with the full search result. Which is the same as not doing anything.
^([^:\-\/\D]+|\d+\.\d+)$
With your examples this will leave the non-numbers if we replace with $1.
See regex101 playground here:
https://regex101.com/r/VV68Pj/1
Remove the non-numbers
Regular expressions are not for finding a pattern you then want the opposite of the matches to work on. Se we have to find the patterns we don't want to replace them with an empty string. We can classify the non-numbers separately with |:
^((?![\d]+).+|\d+\/\d+\/\d+|\d+-\d+-\d+ \d+:\d+:\d+)$
?! is a negative lookahead, in our case it finds a non-digit
(?![\d]+).+: If the following does not have a digit in it...
\d+\/\d+\/\d+: Or the following is a date (I escaped / there, you may not need to)...
\d+-\d+-\d+ \d+:\d+:\d+: Or the following is a date + timestamp
We then simply replace with nothing (an empty string) to remove them.
Regex101 playground to tinker with it:
https://regex101.com/r/ZK5PDZ/1
I would like to construct a regex that matches two groups, with the second group consisting of a single character repeated the same number of times as the number of characters in the first group. Something like ^(\w+) (x{length of \1}) so, for example, hello xxxxx and foo xxx would match, but hello xxxxy and foo xxy would not. Is this possible?
The goal here is to match indentation in reStructuredText-style lists, where the second line in a list item should be indented to match the start of the text in the first line, excluding the variable-length numerical list marker. For example,
1. If the number is small,
subsequent lines are typically
indented three spaces.
2. Although if the first line has
multiple leading spaces then
subsequent lines should reflect that.
11. And when the number starts to get
bigger the indent will necessarily
be bigger too.
You can do it if
your regex engine supports conditional patterns and
you're willing to accept a fixed upper bound on the number of repetitions.
In that case you can do something like this:
^(\w)?(\w)?(\w)?(\w)?(\w)? (?(1)x)(?(2)x)(?(3)x)(?(4)x)(?(5)x)
This example will match up to a length of 5.
search for regex where Keep all digits with length of 10-13 digits and delete the rest in notepad++
my regex doesnt work
[^\d{10,13}]
it finds numbers with commas too :(
Searching for
^(?:.*?(\d{10,13}).*|.*)$
and replacing with
\1
you keep just the 10 to 13 digit long numbers (and empty lines).
Remove the empty lines searching for
^\n
and replacing with nothing.
See it in action: RegEx101.
Addressing #WiktorStribiżew's comments: Relying on the sought after numbers to be always surrounded by white space (which has been checked with OP - but not for the potential case, lines to (effectively) hold just numbers) the search expression could be adjusted to
^(?:.*\s(\d{10,13})\s.*|.*)$
still replacing with
\1
to handle comma holding strings of numbers correctly: RegEx101
By the way:
[^\d{10,13}]
is a character class, which matches anything, which is not:
a number, or
any character out of "{10,3}" (without the quotes, but including the curly braces).
Please comment if and as this requires adjustment / further detail.
To match numbers that are not exactly 3 digits long:
\b(\d{1,9}|\d{14,})\b
You can find all 10-13 length stand alone digits like this
(?<!\d)\d{10,13}(?!\d)
What you do then is up to you.
I don`t know how does notepad works, but this I think this is the regex you are looking for: ^([0-9]){10,13}$
A good page to create/test regex: http://regexr.com/
I would like to match an exact number in a string, but my regex keeps matching the exact number if it repeats together.
I have the following string:
SomePrefix1201-21,4,52
And I have the following regex to find a match for 21:
SomePrefix[\d]+-[,\d]*21[,$]*
It will match this string fine.
However, it also matches:
SomePrefix1201-2121,4,52
But I only want it to match if it is the exact number.
The number may exist at the end too, so it is not always following by a comma.
I've been racking my brain like anything
Update
Based on the corrected answer below, I managed to find the exact regex I need, with one addition of a lookahead too.
SomePrefix[\d]+-([\d]*,)*21(?!\d)[,$]*
The [,\d]* part matches any number of digits and commas in any order. What you probably wanted was ([\d]*,)* so that any preceding digits and commas must end in a comma (not a digit, which would become a part of the number).
SomePrefix[^-]+-(\d+,)*(21,|21$)
Match the prefix, followed by one or more non-dash characters, then a dash, then zero or more comma-terminated digit fields, followed either by 21, (and possibly more material) or just 21 anchored to the end.
If the comma-terminated fields can be empty, then of course \d* rather than \d+.
It's not clear that you can widely use the anchor operator $ inside a character class (perhaps some regex implementations have this feature), so I distributed it out into two matches for 21, which looks clear. The 21 can be factored out of this:
(21,|21$) -> 21(,|$)
I'm trying to write a regex that will extract the numbers after directory/ in the following URL:
http://www.website.com/directory/9892639512/alphanum3r1c/some-more-text-here/0892735235
If I know the number of digits, then this regex I wrote works:
directory\/([0-9]{7})\/
However, if I remove the number of digits to match {7}, then the regex stops working.
Live demo: http://regex101.com/r/wX3eI2
I've been trying different, things, but can't seem to get the regex to work without explicitly setting the number of characters to match.
How can I get this working?
Change regex to:
directory\/([0-9]+)\/
The {7} means, 7 characters (in this case only numbers). The + means one or more characters (in this case numbers).