Regex to match an exact number of characters in a string [duplicate] - regex

This question already has answers here:
regular expression to match exactly 5 digits
(5 answers)
Closed 4 years ago.
I'm trying to get a match of 9-digit integer number. The text I'm scanning can be a length of 1-200 character.
The trouble I'm running in to is that I do not want to match if the input has a series of digits longer than 9. I also need to match if the whole input string is 9 digits. Or begins or ends with the 9-digit number.
I've tried:
d{9} > This matches sub-strings longer than 9 digits
d{9}(\D) > This works unless the sub-string is at the end since this expects some character after the 9 digits.
I have search a lot but I have not found this exact issue. Any ideas?
Note: I happen to be working with ColdFusion for this particular issue but I'm hoping that a general regex will do the trick. If necessary I can code this in CFML.

Use alternatives to match a non-digit or beginning/end of the string:
(^|\D)\d{9}($|\D)

Related

How to select everything after the first white space using regex [duplicate]

This question already has answers here:
Regex to get all character to the right of first space?
(4 answers)
Closed 3 years ago.
I'm trying to get this via regex. I have a series of strings that all have the same general structure:
10-1:15 Build Support: (available)
A number, a space, a string following, a colon, and a string.
I need to extract the string portion after the first white space
Build Support: (available)
How can I regex this?
Side note: I'm on a Ruby project so I've been beating my head on https://rubular.com/.
This is something quick I came up on rubular:
https://rubular.com/r/tlfeSPy7zhfRN7
I tried matching with a group of non word characters and then capturing subsequent word characters. Match 2 should be what you're looking for.

Different behavior between two regex patterns [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I am trying to match the letters 'C' or 'c' as they appear in a file.
They must be stand alone and NOT followed by a '+' or '.'.
The following two patterns give me the same result using Regex101, but I get a different result
in the Dataquest IDE and my home PC.
The two patterns are:
pattern = r'\b[Cc]\b(?!\+|\.)'
pattern = r"\b[Cc]\b[^.+]"
The problem line in question is: (Line 223 from the hacker_news.csv file)
MemSQL (YC W11) Raises $36M Series C
On my home PC and Dataquests IDE:
The regex using the negative lookahead matches that line.
The other regex does not.
On Regex101 they both match that line.
I am NOT supposed to match it.
I wrote the lookahead regex, which fails in Dataquests IDE.
The non-lookahead version is their answer, which passes.
I think they should both yield the same result, but they do not.
I am running Python 3.7.6
What am I missing?
(?!\+|\.) is negative lookahead. It doesn't include any additional characters in the match; it simply adds a requirement to the character that precedes it that says it can't be followed by . or +. In your input string, the C at the end is not followed by one of these characters, so the match succeeds.
[^.+] matches a single character that is not a . or a +. There are no characters after the C so the match fails.

Regex pattern for String not ending in a sequence [duplicate]

This question already has answers here:
Regex: match everything but a specific pattern
(6 answers)
Closed 4 years ago.
I am trying to find all strings not ending with the sequence -6X.
What i'm trying to do is compare the value of an xml tag with a sequence.
If it matches then do some stuff.
However I am unable to do so. I want to achieve this without any lookaheads or lookbehinds.
I've tried using .*[^-6X]
However this does not work for strings ending in - or -5 .
I want to ignore strings ending in -6X only and every other pattern should work.
Thanks in advance for your help
This matches Strings NOT ending with -6X:
.*?([^-]..|.[^6].|..[^X])$
https://regex101.com/r/omwkQ3/2

Regex to match exact number [duplicate]

This question already has answers here:
Regex match exact number not if it exist in string
(2 answers)
Closed 2 years ago.
I have a lot of LOC of a project in visual studio and I want to search for every line which uses the numbers 12 and 13. It can't be part of a bigger number, I need to retrieve only the code that actually uses the constants 12 and 13. I think it is possible to do with regex but I'm having a hard time here.
Any help will be very appreciated.
Brief
You want to use the Find and Replace window found at Edit -> Find and Replace -> Find in Files with the regex \b1[23]\b and the Find Options Use Regular Expressions checkbox selected.
Code
\b Word boundary assertion
Matches, without consuming any characters, immediately between a character matched by \w and a character not matched by \w (in either order). It cannot be used to separate non-words from words.
1 Match this literally
[23] Match a character in the set (2 or 3)
\b Word boundary assertion
(?<![0-9])1[23](?![0-9])
Will match
12
13
abc12hbd
but not
3456324123656
234564567546
121212
13121312
1
3
123
If your 12 or 13 might appear in a hexadecimal string you can exclude that with
(?<![0-9a-fA-F])1[23](?![0-9a-fA-F])
You need to decide what characters are allowed to be on either side of the 12 or 13 and then exclude the others. See https://regex101.com/ for more help
This might be a solution:
^\D*(?<p>12|13)\D*
the group with the name p would hold the 12 or 13.
But you better try to use an online regex tester such as https://regex101.com/ or any other that shows up on google.

Regex for uppercase lowercase and some special characters [duplicate]

This question already has answers here:
Regular expression to check if password is "8 characters including 1 uppercase letter, 1 special character, alphanumeric characters"
(14 answers)
Closed 8 years ago.
I want an regex for password which contain following:
It must contain one uppercase letter.
It must contain one lowercase letter.
It also may include symbols but not these:=?<>()'"/\&.
Legth is minimum 8 and maximum 20
Help me please......
Some valid inputs : Abscedsd Ancbdj123 asjkQs23
Some invalid are : asdfghjk Asdfghj& ashhgWhd=?
Try this:
^.*(?=.{8,20})(?=.*[a-z])(?=.*[A-Z])(?=.*[-_]).*$
or just look around in the forum
Have you tried this? - ^(?=.\d)(?=.[a-zA-Z]).{4,8}$
I also found this within this site: ^(?=.[a-z])(?=.[A-Z])(?=.\d)(?=.[$|~=[]'+#.-])[a-zA-Z0-9$|~=[]'+#.-]{8,}$
The entire link is here: Regular expression for password with certain special characters excluding all others
You can use this regex:
^(?=.*?[A-Z])(?=.*?[a-z])(?!.*?[=?<>()'"\/\&]).{8,20}$
Working Online Demo: http://regex101.com/r/lY9iU0
But it will be better in future if you show your own attempts to solve the problem.