Regex to match exact number [duplicate] - regex

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.

Related

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 to match an exact number of characters in a string [duplicate]

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)

RegEx for an S followed by 6 digits

I am using Visual Studio File search with a regular expression to find an alphanumeric string of 7 characters, starting with an S or s and followed by 6 digits. For example:
s123456
S012458
s004580
Is there any easy way of searching for it?
I have already used this one, although I am not sure if it is getting everything as there are lots of files:
[sS]{1}\d{6}
The first remark: Your proposed regex [sS]{1}\d{6} contains unnecessary
{1}, because the default quantifier is just {1}.
Another remark: The "shortened" regex [sS]\d{6} can capture a fragment
of a longer word, like xs123456 (extra chars before s) or S01245856
(more than 6 digits).
To protect against such cases, you should add a word boundary marker - \b,
both at the start and at the end of the regex.
So the final version is: \b[sS]\d{6}\b.

Regex - Looking for a number either at the BOL or EOL but also not embed in other numbers or text [duplicate]

This question already has answers here:
Regex match entire words only
(7 answers)
Closed 5 years ago.
I want to find '106'. But it can't be in a longer number like 21067 nor can it be in a text string like y106M. But I also want to find it if it is at the beginning, or the end of line, or if it is the line.
So these lines are out:
106M
1106
106in a string is bad even if it starts the line
And it may finish with the target but not as the tail of a string106
but these are in:
106 something else
another 106
And of couse "106' will work as well
I just want 106 to show up but not embedded in other numbers or text
but it's OK is the end of the line is 106
106 may also start the line
I have tried the following search string and many variations of it:
(^|[^0-9A-Za-z])106($|[^0-9A-Za-z])
I'm new-ish to regex, and I likely don't have a handle on how the anchor characters work.
You can just use \b word boundary assertion:
\b106\b
Demo
Or be more specific with lookarounds:
(?<=^|\W)106(?=$|\W)
Demo
or, as pointed out in comments, (?<!\w)106(?!\w) works too. Or (?<=[^a-zA-Z0-9])106(?=[^a-zA-Z0-9]) will work even better since _ is included in \w and \W set of characters.

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.