Visit Our UNIX and Linux User Community


All combinations from simple regex


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers All combinations from simple regex
# 1  
Old 05-24-2013
All combinations from simple regex

Hi !

Before trying to write a code, is there any program or code that generates all the combinations of strings that simple awk regex can match.
By "simple regex" I mean let's say without "+", "*", and with a limited number of characters (e.g. from "1" to "5").

e.g:
input:
Code:
[12]34?5

output:
Code:
1345
135
2345
235

Thanks !
# 2  
Old 05-24-2013
Well, for starters, you could run a loop from the minimum number to the maximum number (the range in which regex would possibly match) and print all the numbers that match the regex.

Code:
[user@host ~]$ awk 'BEGIN{for (i=135; i<=2345; i++){if (i ~ /[12]34?5/){print i}}}'
135
235
1135
1235
1345
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
2135
2235
2345
[user@host ~]$

# 3  
Old 05-24-2013
@bala: try:
Code:
awk 'BEGIN{for (i=1; i<=99999; i++){if (i ~ /[12]34?5/){print i}}}'

This with up to 5 numbers, otherwise there is an infinite number of numbers that this extended regex will match ..
# 4  
Old 05-24-2013
I am not sure but with the example above only 4 combinations are possible, no?

"1" and "2" can be matched only once each.
Or maybe I should precise:

Code:
[12]{1}34?5

Correct me if I am wrong!
# 5  
Old 05-24-2013
Quote:
Originally Posted by Scrutinizer
@bala: try:
Code:
awk 'BEGIN{for (i=1; i<=99999; i++){if (i ~ /[12]34?5/){print i}}}'

This with up to 5 numbers, otherwise there is an infinite number of numbers that this extended regex will match ..
@Scrutinizer: Looks like OP wants exactly those numbers that match the pattern and not the numbers containing digits that match the pattern.

Quote:
Originally Posted by beca123456
I am not sure but with the example above only 4 combinations are possible, no?
True, four numbers would exactly match that pattern. A small tweak to the regex would do the trick. Use start and end anchors, viz. ^ and $

Code:
awk 'BEGIN{for (i=135; i<=2345; i++){if (i ~ /^[12]34?5$/){print i}}}'

# 6  
Old 05-24-2013
Yes that is what I mean, you need those anchors, ( ^ and $ ) otherwise it will match many more numbers..
# 7  
Old 05-24-2013
Quote:
@Scrutinizer: Looks like OP wants exactly those numbers that match the pattern and not the numbers containing digits that match the pattern.
Yep ! Sorry I should have been more precise in my explanations.
Another example to clarify:

input:
Code:
[ab]cd?e

output:
Code:
acde
ace
bcde
bce

 

Previous Thread | Next Thread
Test Your Knowledge in Computers #898
Difficulty: Medium
The Toybox project was started in 2012 and became a BSD-licensed BusyBox alternative.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

2. Shell Programming and Scripting

All possible combinations problem

Post #2 is the original post. This is the first answer to post #2 Hi, try: awk ' { match($0,/1+/) b=substr($0,1,RSTART-1) e=substr($0,RSTART+RLENGTH,length) for(i=2^RLENGTH-2; i>0; i--) { s=x; d=i while(d) { s=(d%2==0?0:1) s ... (12 Replies)
Discussion started by: Scrutinizer
12 Replies

3. Shell Programming and Scripting

All possible combinations

Hi, I have an input file like this a b c d I want to print all possible combinations between these records in the following way aVSb aVSc aVSd bVSc bVSd cVSd VS indicates versus. All thoughts are appreciated. (5 Replies)
Discussion started by: jacobs.smith
5 Replies

4. Shell Programming and Scripting

Combinations / Permutations

Hello Scrutinizer / Group , The shell script of awk that Scrutinizer made calculate all possible permutations in this case 3125 (5 numbers) but i want to have only the 126 possible combination. For now it does not matter the specific order of the combination numbers. I would appreciate it you... (1 Reply)
Discussion started by: csierra
1 Replies

5. Shell Programming and Scripting

help with simple regex expression

I am trying to grep the following line in a file using a bash shell: (..) admin1::14959:::::: (..) It works with the following expression (as expected) # cat file | grep ^*:: admin1::14959:::::: but it does not work with (not expected) # cat /etc/shadow | grep ^+:: I assume the... (2 Replies)
Discussion started by: schms
2 Replies

6. Shell Programming and Scripting

Help with simple RegEx on grep

Hello, I am trying to grep my log files for ORA errors, except ORA-00001. I have tried: grep 'ORA*!(-00001)' *.log but it is not working. Any help will be much appreciated. Thank you. (5 Replies)
Discussion started by: drbiloukos
5 Replies

7. Shell Programming and Scripting

Simple regex problem?

Hi all, I am looking to create words from a sentence which adhere to a custom search pattern from my website: Example: ! +! / += ~ where the terms ! = not, +! = AND NOT, += - and equals and ~ = can be like.... Now here is the issue...i want to split a sentence like the one above on... (1 Reply)
Discussion started by: muay_tb
1 Replies

8. Shell Programming and Scripting

Need Help with Simple Regex

I have got a question. How to do this? I mean AND expression in regex. List all the files in current directory that do not contain the words use AND take. Thx.:p (15 Replies)
Discussion started by: evilfreakz
15 Replies

9. Shell Programming and Scripting

A simple find and replace without using any regex (bash)

Hi, I need to do an exact find and replace (I don't want to use regular expressions because the input comes from user). I want to find a line that matches the user's input text and replace it with an empty string. For example, let's say the user enters I love "Unix" and the contents of the... (2 Replies)
Discussion started by: srikanths
2 Replies

10. UNIX for Advanced & Expert Users

Combinations

Hello All, i have two files, one of the format A 123 B 124 C 234 D 345 And the other A 678 B 789 C 689 D 567 I would like to combine them into one file with three columns: A 123 678 B 124 789 C 234 689 (4 Replies)
Discussion started by: Khoomfire
4 Replies

Featured Tech Videos