I'm trying to formulate a grep regex expression that selects only passwords that start with a number and ends with a number. The format of the txt file is:
password, #OfUsersWhoUseThisPassword
(The comma is included) for example:
123456, 25969
12345678, 8667
1234, 5786
qwerty, 5455
dragon, 4321
Regex:
^[0-9]{1}.*[0-9]{1}(?=,)
Demo
Try this :
grep -oP '^\d.*?\d(?=,)' /tmp/file
-o is to print only what is matching
-P is to use perl like regex
Check look around. Check online with explanations
I need a regex to select a part of a string. Possible strings:
"Til.: 1231231231 Fax: 1231231231 Kin.: 1231231231"
"Til.: 1231231231"
"Til.: 1231231231 Kin.: 1231231231"
In all these occasions I need to select the 10 digits following the "Fax: " string if it exists.
You can use this regex :
Fax:\s?(\d{10})
As you didn't precise the language I can only suggest you to test it in the console of your browser; type this :
"Til.: 1231231231 Fax: 1231231231 Kin.: 1231231231".match(/Fax:\s?(\d{10})/)
With grep
grep -oP "(?<Fax: )[^ ]+"
what would be the best regEx to extract all the number (only numbers) before a search string ?
ABC Y C S 1 $ 46CC MAN 25/ 31
Need to extract 25 in this case, but its not fixed length ? Any help ?
'\d+(?=/)'
should work. see test with grep:
kent$ echo "ABC Y C S 1 $ 46CC MAN 25/ 31 "|grep -Po '\d+(?=/)'
25
Perl regex:
while ($subject =~ m!\d+(?=.*/)!g) {
# matched text = $&
}
Output:
1
46
25
So basically keep matching, as long as a / exist somewhere later.
I'm trying to use Notepad++ to do some find and replace as I'm dealing with up to few thousand of lines of data.
The below is the example of the data structure that i am dealing with.
A = Can be any Aplabet
X = Can be any Number 0-9
RX = Number that I want to replace with another value.
AAAAA X.XXXXXX X.XXXXX X X X X X XX:XX:XX:XX.XXX XXX RXRXRXRXRXRX XXXXXX XXXXXX
Actual Example
werwer 2.178924 1.17892 1 1 1 1 1 12:14:44:59.123 123 0123123 123345 123123
gret 2.178975 1.15731 1 1 1 1 1 12:14:44:59.123 123 0123 123345 123123
sdfwe 2.123245 1.15171 1 1 1 1 1 12:14:44:59.123 123 0555312 123345 123123
Is there a shortcut I can use?
N++ is not the tool for the job as it has very limited regexp capabilities. In a decent editor, you could replace
((?:[a-zA-Z0-9:\.]+\s+){10})\d+(.*)
with
\1your_text\2
but notepad++ regex syntax supports neither (?:) nor {10}.
There are lots of regex tools out there, choose whichever.
P.S. I also tried repeating the first pattern ten times to emulate {10}, it still did not work strangely.
This looks like the sort of job that is perfect for awk.
awk '{print "$1 $2 $3 $4 $5 $6 $7 $8 $9 NUMBERS I'\''M CHANGING $11 $12"}' < file.txt > newfile.txt
You can also try vim's Block Highlighting and Insert/Change functionality. I don't know if notepad++ has anything similar to it.
press ctrl+F and then go to Replace tab.
refer to https://superuser.com/questions/214079/find-and-replace-using-notepad
whitequark solution
I would like to get the phone numbers from a file. I know the numbers have different forms, I can handle for a single one, but don't know how to get a uniform regex. For example
xxx-xxx-xxxx
(xxx)xxx-xxxx
xxx xxx xxxx
xxxxxxxxxx
I can only handle 1, 2, and 4 together
grep '[0-9]\{3\}[ -]\?[0-9]\{3\}[ -]\?[0-9]\{4\}' file
Is there any one single regex can handle all of these four forms?
grep '\(([0-9]\{3\})\|[0-9]\{3\}\)[ -]\?[0-9]\{3\}[ -]\?[0-9]\{4\}' file
Explanation:
([0-9]\{3\}) three digits inside parentheses
\| or
[0-9]\{3\} three digits not inside parens
...with grouping parentheses - \(...\) - around the alternation so the rest of the regex behaves the same no matter which alternative matches.
There are usually four patterns of phone numbers
1. xxx-xxx-xxxx grep -o '[0-9]\{3\}\-[0-9]\{3\}\-[0-9]\{4\}' file.txt
2. (xxx)xxx-xxxx grep -o '([0-9]\{3\})[0-9]\{3\}\-[0-9]\{4\}' file.txt
3. xxx xxx xxxx grep -o '[0-9]\{3\}\s[0-9]\{3\}\s[0-9]\{4\}' file.txt
4. xxxxxxxxxx grep -o '[0-9]\{10\}' file.txt
In all
grep -o '\([0-9]\{3\}\-[0-9]\{3\}\-[0-9]\{4\}\)\|\(([0-9]\{3\})[0-9]\{3\}\-[0-9]\{4\}\)\|\([0-9]\{10\}\)\|\([0-9]\{3\}\s[0-9]\{3\}\s[0-9]\{4\}\)' file.txt
Of course, one could simplify the regex above but we can also leave this simplification to grep itself ~
This is just a modified version of Alan Moore's solution. This is protected against some race condition where the last part of the number has more than four digits in it or the if the total number of digits are more than 10:
grep '\(\(([0-9]\{3\})\|[0-9]\{3\}\)[ -]\?\)\{2\}[0-9]\{4\} '
Explanation:
\(([0-9]\{3\})\|[0-9]\{3\}\) matches exactly three digits (e.g. 234)
with or without surrounded by parentheses. \| performs the 'OR' operation.
The first \( ... \) groups together the above format followed by a space or - or no space at all - ([ -]\?) does that.
The \{2\} matches exactly two occurrences of the above
The [0-9]\{4\} ' matches exactly one occurrence for a 4 digit number followed by a space
And it's a bit shorter as well. Tested on RHEL and Ubuntu. Cheers!!
You can just OR (|) your regexes together -- will be more readable that way too!
My first thought is that you may find it easier to see if your candidate number matches against one of four regular expressions. That will be easier to develop/debug, especially as/when you have to handle additional formats in the future.
grep -P '[0-9]{3}-[0-9]{3}-[0-9]{3}|[0-9]{3}\ [0-9]{3}\ [0-9]{3}|[0-9]{9}|\([0-9]{3}\)[0-9]{3}-[0-9]{3}'
Try this one:
^(\d{10}|((([0-9]{3})\s){2})[0-9]{4}|((([0-9]{3})\-){2})[0-9]{4}|([(][0-9]{3}[)])[0-9]{3}[-][0-9]{4})$
This is only applicable for the formate you mention above like:
xxxxxxxxxx
xxx xxx xxxx
xxx-xxx-xxxx
(xxx)xxx-xxxx
We can put all the required phone number validations one by one using an or condition which is more likely to work well (but tiresome coding).
grep '^[0-9]\{10\}$\|^[0-9]\{3\}[-][0-9]\{3\}[-][0-9]\{4\}$\|^[0-9]\{3\}[ ][0-9]\{3\}[ ][0-9]\{4\}$\|^[(][0-9]\{3\}[)][0-9]\{3\}[-][0-9]\{4\}$' phone_number.txt
returns all the specific formats :
920-702-9999
(920)702-9999
920 702 9999
9207029999
+?(1[ -])?((\d{3})[ -]|(\d{3}[ -]?)){2}\d{4}
works for:
123-678-1234
123 678 1234
(123)-678-1234
+1-(123)-678-1234
1-(123)-678-1234
1 123 678 1234
1 (123) 678 1234
grep -oE '\(?\<[0-9]{3}[-) ]?[0-9]{3}[ -]?[0-9]{4}\>'
Matches all your formats.
The \< and \> word boundaries prevent matching numbers that are too long, such as 123-123-12345 or 1234-123-1234
I got this:
debian:tmp$ cat p.txt
333-444-5555
(333)333-6666
123 456 7890
1234567890
debian:tmp$ egrep '\(?[0-9]{3}[ )-]?[0-9]{3}[ -]?[0-9]{4}' p.txt
333-444-5555
(333)333-6666
123 456 7890
1234567890
debian:tmp$ egrep --version
GNU grep 2.5.3
Copyright (C) 1988, 1992-2002, 2004, 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
debian:tmp$