How to not print the lines that have more than a certain number of characters - regex

I am trying to print the lines that have numbers that are either greater or equal to 3 and less than or equal to 6. The problem is that the regular expression I have prints out the files that are 3 or more and not 3 to 6.
What I am trying to get
Valid:
123
34564
234552
Invalid:
12
1453756
What I am getting
Valid:
123
34564
234552
1453756
Invalid:
12
/[0-9]{3,6}/

I tried the following, and it worked for me :
\d{3,6}

Try to use following
^[\d]{3,6}[^\d]
It is working as specified in your requirement

Related

Regex to return integers only without surrounding letters or decimals

As the question says so that in the following string:
"2 x 500ml 1664 êcole beer £8.0"
i would get 2 and 1664 returned only.
Ive tried: [1-9][0-9]* which is close but returns the 500 in 500ml and the 8 in #8.0
The idea is to return the quantity when reading a line in a receipt, hence the example above.
Using Python, I would try to get the each number separately like this:
First occurrence:
"(\d{+})\ x"
Second occurrence:
"\ (\d{4})\ "
Now you'll just have to find a way to get the numbers that are inside each group.. ;-)
EDIT:
You can get the numbers using a one-line solution, and then fetch each subgroup to access the numbers.
One-line Solution:
"(\d+)\ "

Remove "+" from output in SAS Content Catecorization

I got a little problem in SAS Content Categorization. I'm working with getting out two values. Value 1 and value 2.
I use predicate_rule, so when I click on the matched string in the program I get
ARGUMENT 0 [val1]: 4
ARGUMENT 1 [val2]: 4
ARGUMENT 2 [valName]: Score
In this example 4 is just an example of a value, but my problem is that when it stand 4+4 (no space between 4, + and 4) I can't get the latest value WITHOUT the plus symbol, so I get this out
ARGUMENT 0 [val1]: 4
ARGUMENT 1 [val2]: +4
ARGUMENT 2 [valName]: Score
I only manage to get the value printet correctly if there is space between the numbers and plus symbol.
I have now crateded two regex and two predicate_rules.
This one is for the first value (val1), called: Regex1
REGEX:[1-5]
This is for the seconed value (val2), called: Regex2
REGEX:\+[1-5]
I know that I get the plus symbol printed out because of Regex2, but I can't manage to get the latest value without typing it this way.
In the main concept I have created two predicate_rules. One that should manage the score which have space between the numbers and the plus symbol, and one that should manage when there is no space between.
#With space
PREDICATE_RULE:(valName,val1,val2):(ORDDIST_4, "_valName{valName}", "_val1{Regex1}", "+", "_val2{Regex1}")
#Without space
PREDICATE_RULE:(valName,val1,val2):(ORDDIST_3, "_valName{valName}", "_val1{Regex1}", "_val2{Regex2}")
valName only contains terms that should be in distance of the arguments so it matches correctly.
Thanks in advance.
I think you can look at altering your 2nd regex in the predicate_rule. Since you mentioned that text pattern like 4+4 is an issue. You could probably look into Positive lookbehind to solve the issue. Positive lookbehind will help you to select your group before your main expression without including it in the result.
Pattern like below could easily solve by Positive lookbehind:
4+4
4 + 4
4 +4
4 4
Try the following regex for the 2nd predicate_rule :
(?<=[\+ ])[\d]

Python 2 - printing raw input into an array, formatting output

I'd like to take raw input from a user and then re-arranging the output in a different order. I have the code working, but the output has single quotes around each number. Here is the really simple code:
CodeNumber=(raw_input("Please enter 4 digit number: "))
print "Code_A: %r%r%r" %(CodeNumber[3], CodeNumber[1], CodeNumber[0])
print "Code_B: %r%r%r" %(CodeNumber[2], CodeNumber[3], CodeNumber[1])
The output I get is:
Please enter 4 digit number: 1234
Code_A: '4''2''1'
Code_B: '3''4''2'
How do I remove the single quotation marks so I get 421 instead of '4''2''1'?
At least one way to do this is to use int() on element indices of CodeNumber to turn the strings into numbers.
This is your edited code (by me, hob!)
CodeNumber=(raw_input("Please enter 4 digit number: "))
print "Code_A: %r%r%r" %(int(CodeNumber[3]), int(CodeNumber[1]), int(CodeNumber[0]))
print "Code_A: %r%r%r" %(int(CodeNumber[2]), int(CodeNumber[3]), int(CodeNumber[1]))
Output:
Please enter 4 digit number: 1234
Code_A: 421
Code_B: 342
Running time = O(n)

Vim Regex gives incorrect output

I have a list of dates (YYYY-M or YYYY-MM) and want to prefix 0 before the first 9 months for consistency. Data format : Date in YYYY-M or YYYY-MM followed by a comma and a number.
Eg:
2012-1,789
2012-11,563
2012-1,789 should be changed to 2012-01,789. The entry `2012-11,563' should remain unchanged.
Correct output should be:
2012-01,789
2012-11,563
I tried following regular expression in Vim.
:%s/-\(\d\),/-0\0,/g
However, I get the following output:
2012-0-1,789
2012-11,563
Why am I getting an additional dash - between two digits?
Capturing group number starts from 1, not from 0.
So the command should be:
:%s/-\(\d\),/-0\1,/g

Regex for specific number

I need regular expression for ID number,
it's 9 numbers and the first number must be 4 or 9 or 8.
can some one help ?
You can use the following regex:
^[489][0-9]{8}$