regex replace preceding zeros with equal amount of space - regex

I am searching for a regexp that replaces preceding zeros with equal amount of spaces in notepad++.
^(0+) gets all the zeros but how can i replace them with an equal amount of spaces?
00000000
00000001
00000072
00000073
00000070
00001105
00000176
should become
0
1
72
73
70
1105
176

(?<![1-9])0(?=\d)
Then just replace with the space character

Related

Regex Get Value before string or number

I would like to ask is that possible to get digit or string after The word.
e.g
Abc ddd 1 0.4 44 USD 99 00 99
cc gg 1 0.4 445 66 USD 100 00 999
bb dd xx cc 56 78 99 65 35.45 USD 99 00 88 66 99
Out put target any value before USD
44
66
35.45
meaning that any value before USD.
Try using below code but couldnt get
(?<=USD\s\d+)\S+
Your regex attempts to match one or more non-whitespace chars that are immediately preceded with USD + a whitespace + one or more digits. However, you need to get the numbers that are to the left of the USD substring.
You can use a lookahead based solution here:
\d+(?:\.\d+)?(?=\s+USD\b)
See the regex demo.
Details:
\d+(?:\.\d+)? - one or more digits followed with an optional sequence of a dot and one or more digits
(?=\s+USD\b) - the positive lookahead makes sure there are
\s+ - one or more whitespace chars
USD\b - and an USD substring as a whole word (as \b is a word boundary).

Regex capture consecutive numbers in a pattern

Trying to extract the numbers from a string of pattern:
<Some Alphanumeric> <numbers> X <numbers> <Some Alphanumeric>
e.g.
I 00 Crazy 060 X 0140 08 Dance 47
should extract the numbers 060 and 0140 and the text I 00 Crazy and 08 Dance 47
I'm using the following Regex:
(.*)(\d{1,3})\s*(x|X)\s*(\d{1,4})(.*)
However this isn't working on the first number preceding the X, it's only capturing 0 instead of 060 but captures the second number 0140 correctly.
\d{1,3} should be a greedy capture of digits between 1 and 3 in length - so what am I missing here?
This should work,
(.*)\b(\d{1,3})\s*(x|X)\s*(\d{1,4})(.*)
Here, \b asserts position at a word boundary (^\w|\w$|\W\w|\w\W)

Regular Expression to match amounts

I have this regular expression:
^-?([0-9]{1,3})+([ 0-9]{3})*([\.0-9]{2})?$
Format should be marked as valid:
190 254 254
10 254 254
1 254 982
250 254
10 254
1 154
190 254 254.22
10 254 254.22
1 254 982.22
250 254.22
10 254.22
1 154.22
-190 254 254
-10 254 254
-1 254 982
-250 254
-10 254
-1 154
-190 254 254.22
-10 254 254.22
-1 254 982.22
-250 254.22
-10 254.22
-1 154.22
But after I tested it here I got only partial matching.
UPDATE:
After correcting the regular expression by Mr #anubhava, the QLineEdit now accepts other formats too:
4654d654
55d54
444444
These is how I validate the input:
QRegExp rx("^-?[0-9]{1,3}(?: [0-9]{3})*(?:\.[0-9]{0,2})?$");
QValidator *currencyValidator = new QRegExpValidator(rx, this);
ui->unitPrice->setValidator(currencyValidator);
It turns out that I didn't escape the backslash:
QRegExp rx("^-?[0-9]{1,3}(?: [0-9]{3})*(?:\\.[0-9]{0,2})?$");
^
You can fix it by modifying your regex to this:
^-?[0-9]{1,3}(?: [0-9]{3})*(?:\.[0-9]{0,2})?$
Rather than keeping space and DOT inside the character class match them before the character classes.
Updated Regex Demo
This segment ([0-9]{1,3})+ means:
( Start of capturing group
[0-9] Match digit
{1,3} Match 1-3 of previous (digit)
) End of capturing group
+ Match 1 or more of previous (capturing group)
The result is that it will match 1 or more digits, capturing the last 1-3 digits.
Since {1,3} is greedy, it prefers matching 3, so for input 12345678, that means:
123 First repetition of capturing group
456 Second repetition of capturing group
78 Third repetition of capturing group
And since only the last repetition of the group is actually captured, you get 78, which is not what you want. See this regex101 for more info.
That was just the first of three segments of your regex. All three segments are mixing {n,m} with + or *. + is just shorthand for {1,}, * is shorthand for {0,}, and ? is shorthand for {0,1}.
So, x{1,3}+ really means x{1,3}{1,}, and that makes no sense, so stop doubling the repetitions.
So, what should your regex be? Probably something like this:
(-?[0-9]{1,3})(?: ([0-9]{3}))? ([0-9]{3}(?:\.[0-9]{2})?)
For input -190 254 254.22, that will return -190, 254, and 254.22. See this regex101 for full test.

PCI Compliance regex detect pattern with spaces

I have to generate a regular expression to detect patterns of text where credit card numbers are involved, I have a regular expression but fails when the text is altered with simple spaces between the text for example (not valid credit card number):
4320 7589 9456 0123
The regex is:
4\d{3}(\s+|-)?\d{4}(\s+|-)?\d{4}(\s+|-)?\d{4}
This regex match easy, but if someone alter the text with spaces between any number like this:
4 320 7589 9456 0123
Does not match, I need a regex to detect any possible variable with spaces, special symbols, letters, some examples:
43 20 75 89 94 56 01 23
4 3 2 0 7 5 8 9 9 4 5 6 0 1 2 3
4320a7589b9456c0123
4320$7589$9456$0123
4320_7589_9456_0123
I don't know if I can strip any space, symbols from the pattern to analyze the text?
I am posting because you actually asked for help with pattern to match any number of non-digits between the first 4 and 15 more digits.
The pattern is
^4(?:\D*\d){15}$
See demo
Regex breakdown:
^ - start of string
4 - literal 4
(?:\D*\d){15} - 15 occurrences of sequences of...
\D* - 0 or more non-digit symbols before..
\d - a digit
$ - end of string
If you need to capture, you can capture (like ^4((?:\D*\d){3})((?:\D*\d){4})((?:\D*\d){4})((?:\D*\d){4})$), but the submatches will still contain the "junk" in-between digits.

Notepad++ regex to remove spaces between specific characters but not ALL spaces

I thought my regex skills were strong, but I'm getting crushed.
I have several lines in thsi format
Time: 105 0 0
Time: 88 0 1
Time: 44 1 1
Time: 64 1 0
I want theses to turn into this:
Time: 105 thread00
Time: 88 thread01
Time: 44 thread11
Time: 64 thread10
I can match the [0-9][ ][0-9] section... I match it with that regex right there!
But I don't know how to preserve the values AND remove the space. Replacing it wholesale with new stuff, sure... but how do I PRESERVE values?
Find what: (\d)\s(\d)$
Replace with: thread\1\2
\d matches any digit, \s matches any space character.
The parentheses will be captured for use as \1, \2, \3... and \0 will provide the entire match.*
$ matches the end of a line, so that you don't accidentally match the "5 0" in the first line.
*Note that some regex engines use the \1 pattern while some others will use $1. Notepad++ uses the former.
You can try this:
Pattern:
/^(.*)(\d+)\s(\d+)$/
Breakdown:
^ # start of line
(.*) # the first part of the line -- capture $1
(\d+) # the first number (1 or more) -- capture $2
\s # the space between the numbers
(\d+) # the second number (1 or more) -- capture $3
$ # end of line
Replace:
/$1thread$2$3/
Result:
Time: 105 thread00
Time: 88 thread01
Time: 44 thread11
Time: 64 thread10
Demo: http://regex101.com/r/gB8uS4