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).
Related
I have a simple question.
I need a regular expression to match a hexdecimal number without colon at the end.
For example:
0x85af6b9d: 0x00256f8a ;some more interesting code
// dont match 0x85af6b9d: at all, but match 0x00256f8a
My expression for hexdecimal number is 0[xX][0-9A-Fa-f]{1,8}
Version with (?!:) is not possible, because it will just match 0x85af6b9 (because of the {1,8} token)
Using a $ also isn't possible - there can be more numbers than one
Thanks!
Here is one way to do so:
0[xX][0-9A-Fa-f]{1,8}(?![0-9A-Fa-f:])
See the online demo.
We use a negative lookahead to match all hexadecimal numbers without : at the end. Because of {1,8}, it is also necessary to ensure that the entire hexadecimal number is correctly matched. We therefore reuse the character set ([0-9A-Fa-f]) to ensure that the number does not continue.
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+)*\]
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/
Is there a way to match a fixed number of characters in a fixed length string via regex?
Example, I want to match all strings where the length of string is 5 and there are exactly 3 alphabets and 2 exclamations (!). The exclamations can be anywhere in the string.
Example matches: abc!!, a!b!c, !!abc, a!!bc
I tried to match using lookahead but I wasn't able to limit the length. The following was the regex I used.
(?=\w*!\w*!\w*)[\w!]{5}
This matches a!!!b and a!!!! as well which I don't want.
You can do this using a lookahead based regular expression.
^(?=(?:\w*!){2}\w*$)[\w!]{5}$
Live Demo
Probably easiest to just specify all possibilities.
(?=\w\w\w!!|\w\w\!\w\!|\w\w\!!\w|\w!\w\w!|\w!\w!\w|\w!!\w\w|!\w!\w\w|!!\w\w\w)
Regex doesn't work well with combinations/permutations.
If the number of combinations is too large, do it in parts where the first regex gathers potential matches and the second (and beyond) continue to validate it.
[\w!]{5}
match.count('!') == 2
match.count('\w') == 3
(that isn't valid code -- just a concept)
I need a regular expression to match phone numbers. I just want to know if the number is probably a phone number and it could be any phone format, US or international. So I developed a strategy to determine if it matches.
I want it to accept the following characters: 0-9 as well as ,.()- and optionally start with a + (for international numbers). The string should not match if it has any other characters.
I tried this:
/\+?[0-9\/\.\(\)\-]/
But it matches phone numbers that have + in the middle of the number. And it matches numbers that contain alpha chars (I don't want that).
Lastly, I want to set the minimum length to 9 characters.
Any thoughts?
Thanks for any help, I'm obviously not too swift on RegEx stuff :)
Well, you're pretty close. Try this:
^\+?[0-9\/.()-]{9,}$
Without the start and end anchors you allow partial matching, so it can match +123 from the string :-)+123.
If you want a minimum of 9 digits, rather than any characters (so ---.../// isn't valid), you can use:
^\+?[\/.()-]*([0-9][\/.()-]*){9,}$
or, using a lookahead - before matching the string for [0-9/.()-]* the regex engine is looking for (\D*\d){9}, which is a of 9 digits, each digit possibly preceded by other characters (which we will validate later).
^\+?(?=(\D*\d){9})[0-9\/.()-]*$
The reason why it matches alpha character is because of the period. You have to escape it. I don't know what editor you are using for this, this is what I'll use for VIM:
^+\?[()\-\.]\?\([0-9][\.()\-]\?\)\{3,\}$
The juqeury has a plugin for US phone validation. Check this link. You can also see the regular expression in the source code.