Regular expression with two different patterns in tcl - tcl

I am new to tcl and I have the following problem:
Given a text, which consists of of words und usual punctuation, I would like to write a regular expression which searches for words on the one hand and special characters on the other hand. A word is either a sequence of letters ([A-Za-z]) or a sequence of printable letters but a mixture of both is not allowed. It may not have control characters.
The regular expression should have the following syntax:
regexp -all -inline {[A-Za-z]+ .......} $Text
I do not know how to write correctly the search pattern {......}.
Any help is welcome.
Thanks in advance!

If my guess that by 'printable letters' you actually mean punctuation is correct, you could use:
regexp -all -inline {[[:alpha:]]+|[[:punct:]]+} $Text
See the documentation at https://www.tcl.tk/man/tcl8.6/TclCmd/re_syntax.html#M31

Through experimentation, I've found that
[:graph:] == [:alnum:] + [:punct:]
So
[:graph:] - [:alpha:] == [:digit:] + [:punct:]
Unfortunately, while that's true for bash regular expressions (ref), it's not true for Tcl: there are several characters (including = and +) that are in [:graph:] but not [:punct:]
We need a larger negative char class
set letters {[[:alpha:]]+}
set graph_non_letters {[^[:cntrl:][:alpha:][:space:]]+}
Then
regexp -all -inline "(?:$letters)|(?:$graph_non_letters)" $line
Using non-capturing parentheses to tidy the output.
Demo:
% set line {[:graph:] == [:alnum:] + [:punct:]}
[:graph:] == [:alnum:] + [:punct:]
% regexp -all -inline "(?:$letters)|(?:$graph_non_letters)" $line
{[:} graph :\] == {[:} alnum :\] + {[:} punct :\]
Some code to inspect which classes a character resides in:
proc output {data} {
set fmt [join [lrepeat [llength $data] {%-6s}]]
puts [format $fmt {*}$data]
}
set cs {alpha alnum upper lower digit xdigit space blank punct cntrl graph print}
set rs [lmap c $cs {format {[[:%s:]]} $c}]
for {set ascii 0} {$ascii < 128} {incr ascii} {
if {$ascii % 32 == 0} then {output [list ascii char {*}$cs]}
set char [format {%c} $ascii]
set results [lmap r $rs {expr {[regexp -- $r $char] ? "Y" : "."}}]
output [list $ascii $char {*}$results]
}
ascii char alpha alnum upper lower digit xdigit space blank punct cntrl graph print
0 . . . . . . . . . Y . .
1 . . . . . . . . . Y . .
2 . . . . . . . . . Y . .
3 . . . . . . . . . Y . .
4 . . . . . . . . . Y . .
5 . . . . . . . . . Y . .
6 . . . . . . . . . Y . .
7 . . . . . . . . . Y . .
8 . . . . . . . . . Y . .
9 . . . . . . Y Y . Y . .
10
. . . . . . Y . . Y . .
11 . . . . . . Y . . Y . .
12 . . . . . . Y . . Y . .
13
. . . . . . Y . . Y . .
14 . . . . . . . . . Y . .
15 . . . . . . . . . Y . .
16 . . . . . . . . . Y . .
17 . . . . . . . . . Y . .
18 . . . . . . . . . Y . .
19 . . . . . . . . . Y . .
20 . . . . . . . . . Y . .
21 . . . . . . . . . Y . .
22 . . . . . . . . . Y . .
23 . . . . . . . . . Y . .
24 . . . . . . . . . Y . .
25 . . . . . . . . . Y . .
26 . . . . . . . . . Y . .
27 . . . . . . . . . Y . .
28 . . . . . . . . . Y . .
29 . . . . . . . . . Y . .
30 . . . . . . . . . Y . .
31 . . . . . . . . . Y . .
ascii char alpha alnum upper lower digit xdigit space blank punct cntrl graph print
32 . . . . . . Y Y . . . Y
33 ! . . . . . . . . Y . Y Y
34 " . . . . . . . . Y . Y Y
35 # . . . . . . . . Y . Y Y
36 $ . . . . . . . . . . Y Y
37 % . . . . . . . . Y . Y Y
38 & . . . . . . . . Y . Y Y
39 ' . . . . . . . . Y . Y Y
40 ( . . . . . . . . Y . Y Y
41 ) . . . . . . . . Y . Y Y
42 * . . . . . . . . Y . Y Y
43 + . . . . . . . . . . Y Y
44 , . . . . . . . . Y . Y Y
45 - . . . . . . . . Y . Y Y
46 . . . . . . . . . Y . Y Y
47 / . . . . . . . . Y . Y Y
48 0 . Y . . Y Y . . . . Y Y
49 1 . Y . . Y Y . . . . Y Y
50 2 . Y . . Y Y . . . . Y Y
51 3 . Y . . Y Y . . . . Y Y
52 4 . Y . . Y Y . . . . Y Y
53 5 . Y . . Y Y . . . . Y Y
54 6 . Y . . Y Y . . . . Y Y
55 7 . Y . . Y Y . . . . Y Y
56 8 . Y . . Y Y . . . . Y Y
57 9 . Y . . Y Y . . . . Y Y
58 : . . . . . . . . Y . Y Y
59 ; . . . . . . . . Y . Y Y
60 < . . . . . . . . . . Y Y
61 = . . . . . . . . . . Y Y
62 > . . . . . . . . . . Y Y
63 ? . . . . . . . . Y . Y Y
ascii char alpha alnum upper lower digit xdigit space blank punct cntrl graph print
64 # . . . . . . . . Y . Y Y
65 A Y Y Y . . Y . . . . Y Y
66 B Y Y Y . . Y . . . . Y Y
67 C Y Y Y . . Y . . . . Y Y
68 D Y Y Y . . Y . . . . Y Y
69 E Y Y Y . . Y . . . . Y Y
70 F Y Y Y . . Y . . . . Y Y
71 G Y Y Y . . . . . . . Y Y
72 H Y Y Y . . . . . . . Y Y
73 I Y Y Y . . . . . . . Y Y
74 J Y Y Y . . . . . . . Y Y
75 K Y Y Y . . . . . . . Y Y
76 L Y Y Y . . . . . . . Y Y
77 M Y Y Y . . . . . . . Y Y
78 N Y Y Y . . . . . . . Y Y
79 O Y Y Y . . . . . . . Y Y
80 P Y Y Y . . . . . . . Y Y
81 Q Y Y Y . . . . . . . Y Y
82 R Y Y Y . . . . . . . Y Y
83 S Y Y Y . . . . . . . Y Y
84 T Y Y Y . . . . . . . Y Y
85 U Y Y Y . . . . . . . Y Y
86 V Y Y Y . . . . . . . Y Y
87 W Y Y Y . . . . . . . Y Y
88 X Y Y Y . . . . . . . Y Y
89 Y Y Y Y . . . . . . . Y Y
90 Z Y Y Y . . . . . . . Y Y
91 [ . . . . . . . . Y . Y Y
92 \ . . . . . . . . Y . Y Y
93 ] . . . . . . . . Y . Y Y
94 ^ . . . . . . . . . . Y Y
95 _ . . . . . . . . Y . Y Y
ascii char alpha alnum upper lower digit xdigit space blank punct cntrl graph print
96 ` . . . . . . . . . . Y Y
97 a Y Y . Y . Y . . . . Y Y
98 b Y Y . Y . Y . . . . Y Y
99 c Y Y . Y . Y . . . . Y Y
100 d Y Y . Y . Y . . . . Y Y
101 e Y Y . Y . Y . . . . Y Y
102 f Y Y . Y . Y . . . . Y Y
103 g Y Y . Y . . . . . . Y Y
104 h Y Y . Y . . . . . . Y Y
105 i Y Y . Y . . . . . . Y Y
106 j Y Y . Y . . . . . . Y Y
107 k Y Y . Y . . . . . . Y Y
108 l Y Y . Y . . . . . . Y Y
109 m Y Y . Y . . . . . . Y Y
110 n Y Y . Y . . . . . . Y Y
111 o Y Y . Y . . . . . . Y Y
112 p Y Y . Y . . . . . . Y Y
113 q Y Y . Y . . . . . . Y Y
114 r Y Y . Y . . . . . . Y Y
115 s Y Y . Y . . . . . . Y Y
116 t Y Y . Y . . . . . . Y Y
117 u Y Y . Y . . . . . . Y Y
118 v Y Y . Y . . . . . . Y Y
119 w Y Y . Y . . . . . . Y Y
120 x Y Y . Y . . . . . . Y Y
121 y Y Y . Y . . . . . . Y Y
122 z Y Y . Y . . . . . . Y Y
123 { . . . . . . . . Y . Y Y
124 | . . . . . . . . . . Y Y
125 } . . . . . . . . Y . Y Y
126 ~ . . . . . . . . . . Y Y
127 . . . . . . . . . Y . .

The non-alphabetic characters are a bit scattered in the character set, which makes writing a regular expression for them a bit annoying. There is [[:graph:]], but that also matches [[:alpha:]] which you want something else doing with. In this case, I'd use a negative lookahead one character wide ((?![[:alpha]])) to constrain an immediately following [[:graph::]] so that the unwanted characters aren't matched; those need to be surrounded in (non-capturing) parens so the + afterwards applies correctly.
Composing things:
set RE {[[:alpha:]]+|(?:(?![[:alpha:]])[[:graph:]])+}
regexp -all -inline $RE $Text
Testing interactively looks OK:
% regexp -all -inline $RE "One 2 {three}, four 56"
One 2 \{ three \}, four 56

Related

How do I handle SPARQL duplicate specific values?

SELECT ?s ?data ?pro ?o
WHERE {
?s rdf:type rdfs:Class .
?data rdf:type ?s .
pro rdf:type rdf:Property .
?data ?pro ?o .}
This SPARQL result is as follows:
?s ?data ?pro ?o
A1 B1 A1.a 0
A1 B1 A1.b 0
A1 B1 A1.c 1
A1 B1 A1.d 1
A2 B2 A2.a 0
A2 B2 A2.b 0
.
.
.
The result I want is
A1 B1 A1.a 0
A1.b 0
A1.c 1
A1.d 1
A2 B2 A2.a 0
A2.b 0
.
.
.
Is this possible?
I understand that DISTINCT is not the solution I want.
Thank you. Happy New Year

Is this simplification correct? (Demorgans Theorem)

{a(b+c)+a’b}’ 
using demorgans theorem I got a'+ b'c'a + b' then I factored b' out of b'c'a + b' to get b'(1+c'a) which just turns into b'. plugging it back into the equation I'm left with a'+b'. Is that correct or do I have this all wrong?
 
{a(b+c)+(a'b)}' = (a (b+c))' . (a'b)'
= (a' + (b+c)') . (a+b')
= (a' + (b'.c')) . (a+b')
= (a.a') + (a'b') + (ab'c') + (b'c')
= 0 + a'b' + b'c'(a+1)
= a'b' + b'c'(1)
= a'b' + b'c'
= b'(a'+c')

Product of sums for a boolean function

F = AB + C'D'
how can i factor out this expression to obtain product of sums ,
i think we have to make up two new terms each with zero
like F = AB + C'D' + AA' + B'B or something of the sort but how to do it exactly
any idea appreciated
F = AB + C'D'
= (A' + B')' + (C + D)' ; De Morgan on each term
= ((A' + B')(C + D))' ; De Morgan again
here is a hint :
Morgans law
AB= ( A' + B' )'
Here is an example
F = ab' + ad + c'd + d'
F'= (ab' + ad + c'd + d')' = (ab')' . (ad)' . (c'd)' . (d')' ---> a'.b' =(a'+b') De Morgans law = (a'+b)(a'+b')(c+d')(d)
Hence Product of sums.
Your case:
F = ab +c'd' = (a'+b')' + (c+d)' = (a'+b').(c+d)
Just use De Morgans law ;)

list all records from one custom taxonomy starting with one letter i.e A

I want to list all records in one custom taxonomy start with only A or B. Below code is to list all record with all letters.
Here is the code to list all record with one custom taxonomy with all letters in groups. Example,
A
Aajkacatch (0)
Aajkiitem (0)
Aamzie (0)
Aaneri (0)
Aaramshop (0)
Abaaya (0)
B
B.LAB (0)
Baby-Republic (0)
Babyoye (45)
Bank-Bazaar (1)
<?php
// Template Name: Store Template
// get all the stores
$stores = get_terms(APP_TAX_STORE, array('hide_empty' => 0, 'child_of' => 0, 'pad_counts' => 0, 'app_pad_counts' => 1));
// get ids of all hidden stores
$hidden_stores = clpr_hidden_stores();
$list = '';
$groups = array();
if ($stores && is_array($stores) ) {
// unset child stores
foreach($stores as $key => $value)
if($value->parent != 0)
unset($stores[$key]);
foreach($stores as $store)
$groups[mb_strtoupper(mb_substr($store->name, 0, 1))][] = $store;
if (!empty($groups)) :
foreach($groups as $letter => $stores) {
$old_list = $list;
$letter_items = false;
$list .= "\n\t" . '<h2 class="stores">' . apply_filters( 'the_title', $letter ) . '</h2>';
$list .= "\n\t" . '<ul class="stores">';
foreach($stores as $store) {
if (!in_array($store->term_id, $hidden_stores)) {
$list .= "\n\t\t" . '<li>' . apply_filters('the_title', $store->name). ' (' . intval($store->count) . ')</li>';
$letter_items = true;
}
}
$list .= "\n\t" . '</ul>';
if(!$letter_items)
$list = $old_list;
}
endif;
} else {
$list .= "\n\t" . '<p>' . __('Sorry, but no stores were found.', 'appthemes') .'</p>';
}
?>
While it wouldn't be the most efficient way, you could easily just check the first letter of the store name.
change:
if (!in_array($store->term_id, $hidden_stores)) {
to:
if (!in_array($store->term_id, $hidden_stores) ) {
$first_letter = substr( $store->name, 0, 1 );
if ( $first_letter == 'A' || && $first_letter == 'B' )
That would ensure you're only listing A and B items.

2 Functions executing side by side

Ok, this is my first time asking a question on here so please don't be mad if it's not a good question. I have 6 functions representing the image of a dice being rolled. I have to make a craps game for programming. The game itself is executing perfectly but I have to have the dice shown side by side as opposed to on top of each other.
For example:
I have this:
+------+
| * |
| * |
+======+
+------+
| * |
| * |
+------+
I need this:
+------+ +------+
| * | | * |
| * | | * |
+------+ +------+
Here is my program so far:
import random
def roll_dice1():
print "+-------+"
print "| |"
print "| * |"
print "| |"
print "+-------+"
def roll_dice2():
print "+-------+"
print "| * |"
print "| |"
print "| * |"
print "+-------+"
def roll_dice3():
print "+-------+"
print "| * |"
print "| * |"
print "| * |"
print "+-------+"
def roll_dice4():
print "+-------+"
print "| * * |"
print "| |"
print "| * * |"
print "+-------+"
def roll_dice5():
print "+-------+"
print "| * * |"
print "| * |"
print "| * * |"
print "+-------+"
def roll_dice6():
print "+-------+"
print "| * * * |"
print "| |"
print "| * * * |"
print "+-------+"
def dice_roll():
counter = 0
dice = []
while counter < int(2):
dice += [random.randint(1,6)]
counter += 1
i = 0
while i < 2:
if dice[i] == 1:
roll_dice1()
elif dice [i] == 2:
roll_dice2()
elif dice [i] == 3:
roll_dice3()
elif dice [i] == 4:
roll_dice4()
elif dice [i] == 5:
roll_dice5()
elif dice [i] == 6:
roll_dice6()
i += 1
roll = dice
return roll
def point_cycle():
raw_input( "Press <Enter> to roll the dice")
roll = dice_roll()
total1 = int(roll[0]) + int (roll[1])
if total1 == 7:
print "You rolled a 7."
print "You lose!"
elif total1 == total:
print "You rolled a " + str(total1)
print "You win!"
else:
print "You rolled a " + str(total1)
point_cycle()
def main():
print "Craps: A Popular Dice Game"
raw_input( "Press <Enter> to roll the dice")
roll = dice_roll()
total = int(roll[0]) + int (roll[1])
if total == 7 or total == 11:
print "You rolled a " + str(total) + " on your first roll."
print "You win!"
elif total == 2 or total == 3 or total == 12:
print "You rolled a " + str(total) + " on your first roll."
print "You lose!"
else:
print "You rolled a " + str(total) + " on your first roll."
print " "
print "Thats your point. Roll it again before you roll a 7 and lose!"
point_cycle()
global total
main()
assuming you are using python, a quick and dirty solution to get you started could be this one:
# Save the dices in a way that allows you to access each line separatedly
DICES = (None,
("+-------+", "| |", "| * |", "| |", "+-------+"), # dice 1
("+-------+", "| * |", "| |", "| * |", "+-------+"), # dice 2
<put here the tuples with the other lines of each dice>)
and use a function like this one:
def print_dices(diceslist):
for i in range(5):
for dicenum in dicelist:
print DICES[dice][i] + " ",
print
to print the dices you want, where diceslist is a list of outcomes, like: (1,3,4)
There are of course far more elegant/optimized solutions, but this one is simple enough and might point you in an acceptable direction