String.replace in Java - replaceall

How can I transform the string
st = "[123, 123, 134, 90]"
into
s = "123 123 134 90"

replace " " with ""
replace "," with " "
replace "[" with ""
replace "]" with ""

With Thanks to Maroun Maroun
String st = "[123, 123, 134, 90]"
String s = st.replaceAll("\\["," ").replaceAll("\\]"," ").replaceAll(","," ");

Related

My input function and len function does not return a correct result in some cases

I have a question I wrote this script to format an arabic text to a certain format. But I found a problem then when I type a longer sentence it adds more dots then there are letters. I will paste the code here and explain what the problem is.
import itertools
while True:
input_cloze_deletion = input("\nEnter: text pr 'exit'\n> ")
input_exit_check = input_cloze_deletion.strip().lower()
if input_exit_check == "exit":
break
# copy paste
interpunction_list = ["الله", ",", ".", ":", "?", "!", "'"]
# copy paste
interpunction_list = [ ",", ".", ":", "?", "!", "'", "-", "(", ")", "/", "الله", "اللّٰـه"]
text_replace_0 = input_cloze_deletion.replace(",", " ,")
text_replace_1 = text_replace_0.replace(".", " .")
text_replace_2 = text_replace_1.replace(":", " :")
text_replace_3 = text_replace_2.replace(";", " ;")
text_replace_4 = text_replace_3.replace("?", " ?")
text_replace_5 = text_replace_4.replace("!", " !")
text_replace_6 = text_replace_5.replace("'", " ' ")
text_replace_7 = text_replace_6.replace("-", " - ")
text_replace_8 = text_replace_7.replace("(", " ( ")
text_replace_9 = text_replace_8.replace(")", " ) ")
text_replace_10 = text_replace_9.replace("/", " / ")
text_replace_11 = text_replace_10.replace("الله", "اللّٰـه")
text_split_list = text_replace_11.split()
count_number = []
letter_count_list = []
index_list = itertools.cycle(range(1, 4))
for letter_count in text_split_list:
if letter_count in interpunction_list:
letter_count_list.append(letter_count)
elif "ـ" in letter_count:
letter_count = len(letter_count) - 1
count_number.append(letter_count)
print(letter_count)
else:
letter_count = len(letter_count)
count_number.append(letter_count)
print(letter_count)
for count in count_number:
letter_count_list.append(letter_count * ".")
zip_list = zip(text_split_list, letter_count_list)
zip_list_result = list(zip_list)
for word, count in zip_list_result:
if ((len(word)) == 2 or word == "a" or word == "و") and not word in interpunction_list :
print(f" {{{{c{(next(index_list))}::{word}::{count}}}}}", end="")
elif word and count in interpunction_list:
print(word, end = "")
else:
print(f" {{{{c{(next(index_list))}::{word}::{count}}}}}", end="")
when I type كتب عليـ ـنا و علـ ـي
the return is {{c1::كتب::...}} {{c2::عليـ::...}} {{c3::ـنا::...}} {{c1::و::..}} {{c2::علـ::..}} {{c3::ـي::..}}
but it should be
{{c1::كتب::...}} {{c2::عليـ::...}} {{c3::ـنا::..}} {{c1::و::.}} {{c2::علـ::..}} {{c3::ـي::.}}
I add a print function the print the len() results and the result is correct but it add an extra dot in some case.
But when I type just a single "و" it does a correct len() function but when I input a whole sentence it add an extra dot and I don't know why.
please help

Catia VBA, How to get "Bill of material" to an array

I want to get 2 parameters in "bill of material".
first "Length" in structure workbench, second is "quantity".
I try to find these 2 parameters in
CATIA.Documents.Item(Document).Product.ReferenceProduct
But can't.
I have an idea. I try find a way to get "Bill of material" into an array.
I found a code write Bill of material to excel file.
On Error Resume Next
Dim productDocument1 As productDocument
Set productDocument1 = CATIA.ActiveDocument
Dim product1 As Product
Set product1 = productDocument1.Product
Dim assemblyConvertor1 As AssemblyConvertor
Set assemblyConvertor1 = product1.GetItem("BillOfMaterial")
assemblyConvertor1.[Print] "XLS", "D:\BOM.xls", product1
How to get "Bill of material" data into an array? Thanks
The length parameter of elements of the structure design apparently only available trough the StrComputeServices
Example:
Sub CATMain()
Dim oRootProduct as Product
Dim oInstanceProduct as Product
Dim oStrWB as Workbench
Dim oStrServices As StrComputeServices
Set oRootProduct = CATIA.ActiveDocument.Product
Set oInstanceProduct = oRootProduct.Products.Item(1)
Set oStrWB = CATIA.ActiveDocument.GetWorkbench("StrWorkbench")
Set oStrServices = oStrWB.StrComputeServices
MsgBox CStr(oStrServices.GetLength(oInstanceProduct))
End Sub
I have developed this code below if it can help you :
https://www.catiavb.net/sourceCodeCATIA.php#getbom
You can find the function to get 'MaLangue' in same website (to return language used by CATIA if necessary). Or you can delete every line who refers to 'MaLangue'. To launch the sub you can write GetBOM(Catia.ActiveDocument.Product) if you want to get the BOM of the root product. Or you can launch for for an other product from the root.
You can then read lines of the txt file (thanks to a stream reader) and split by every vbTab to get your array. The advantage is that you will have a bill of materials that either lists all the parts, or only lists the first level as required by certain customer standards
'Genere la BOM
Public Sub GetBOM(p As Product)
Dim NomFichier As String = My. Computer . FileSystem . SpecialDirectories . Temp & "\BOM.txt"
Dim AssConvertor As AssemblyConvertor
AssConvertor = p. GetItem ( "BillOfMaterial" )
Dim nullstr ( 2 )
If MaLangue = "Anglais" Then
nullstr( 0 ) = "Part Number"
nullstr( 1 ) = "Quantity"
nullstr( 2 ) = "Type"
ElseIf MaLangue = "Francais" Then
nullstr( 0 ) = "Référence"
nullstr( 1 ) = "Quantité"
nullstr( 2 ) = "Type"
End If
AssConvertor. SetCurrentFormat (nullstr)
Dim VarMaListNom( 1 )
If MaLangue = "Anglais" Then
VarMaListNom( 0 ) = "Part Number"
VarMaListNom( 1 ) = "Quantity"
ElseIf MaLangue = "Français" Then
VarMaListNom( 0 ) = "Référence"
VarMaListNom( 1 ) = "Quantité"
End If
AssConvertor. SetSecondaryFormat (VarMaListNom)
AssConvertor. Print ( "HTML", NomFichier, p )
ModifFichierNomenclature (My. Computer . FileSystem . SpecialDirectories . Temp & "\BOM.txt" )
End Sub
Sub ModifFichierNomenclature(txt As String )
Dim strtocheck As String = ""
If MaLangue = "Francais" Then
strtocheck = "<b>Total des p"
Else
strtocheck = "<b>Total parts"
End If
Dim FichierNomenclature As String = My. Computer . FileSystem . SpecialDirectories . Temp & "\BOM_.txt"
If IO. File . Exists (FichierNomenclature) Then
IO . File . Delete (FichierNomenclature)
End If
Dim fs As FileStream = Nothing
fs = New FileStream( FichierNomenclature, FileMode. CreateNew )
Using sw As StreamWriter = New StreamWriter( fs, Encoding. GetEncoding ( "iso-8859-1" ) )
If IO. File . Exists (txt) Then
Using sr As StreamReader = New StreamReader(txt, Encoding. GetEncoding ( "iso-8859-1" ) )
Dim BoolStart As Boolean = False
While Not sr. EndOfStream
Dim line As String = sr. ReadLine
If Left (line, 8 ) = "<a name=" Then
If MaLangue = "Français" Then
line = "[" & Right (line, line. Length - 24 )
line = Left (line, line. Length - 8 )
line = line & "]"
sw . WriteLine (line)
Else
line = "[" & Right (line, line. Length - 27 )
line = Left (line, line. Length - 8 )
line = line & "]"
sw . WriteLine (line)
End If
ElseIf line Like " <tr><td><A HREF=*</td> </tr>*" Then
line = Replace (line, "</td><td>Assembly</td> </tr>", "" ) 'pas fait
line = Replace (line, "</td><td>Assemblage</td> </tr> ", "" )
line = Replace (line, " <tr><td><A HREF=", "" )
line = Replace (line, "</A></td><td>", ControlChars. Tab )
line = Replace (line, "#Bill of Material: ", "" )
line = Replace (line, "#Nomenclature : ", "" )
If line. Contains ( ">" ) Then
Dim lines( ) = Strings. Split (line, ">" )
line = lines( 1 )
End If
Dim lines_( ) = Strings. Split (line, ControlChars. Tab )
line = lines_( 0 ) & ControlChars . Tab & lines_( 1 )
If Strings. Left (line, 2 ) = " " Then line = Strings. Right (line, line. Length - 2 )
sw . WriteLine (line)
ElseIf Left (line, 14 ) = strtocheck Then
sw . WriteLine ( "[ALL-BOM-APPKD]" )
ElseIf line Like "*<tr><td>*</td> </tr>*" Then
line = Replace (line, "<tr><td>", "" )
line = Replace (line, "</td> </tr> ", "" )
line = Replace (line, "</td><td>", ControlChars. Tab )
Dim lines_( ) = Strings. Split (line, ControlChars. Tab )
line = lines_( 0 ) & ControlChars . Tab & lines_( 1 )
If Strings. Left (line, 2 ) = " " Then line = Strings. Right (line, line. Length - 2 )
sw . WriteLine (line)
Else
'nothing
End If
End While
sr . Close ( )
End Using
End If
sw . Close ( )
End Using
End Sub

Object required when using GetList function for concatenation

OBJECTIVE: To concatenate row values
Record in COMP table
player_id cc_number
1 123
2 152
1 254
2 154
3 256
Result to be
player_id cc_number
1 123, 254
2 152, 154
3 256
CODE:
MYSQL = "Select T.player_id, "
MYSQL = MYSQL & GetList("Select cc_number From COMP As T1 Where T1.player_id = " & [T].[player_id], "", ", ") & " AS NewCCNumber"
MYSQL = MYSQL & "From COMP AS T"
MYSQL = MYSQL & "Group By T.player_id"
ERROR:
i used GetList function but it gives me error Object Required.
Need space in front of FROM and GROUP or after NewCCNumber and T. Otherwise text strings run together. Also, the GetList() function should probably be embedded in string
MYSQL = "Select T.player_id, "
MYSQL = MYSQL & "GetList('Select cc_number From COMP As T1 Where T1.player_id = " & [T].[player_id] & "', '', ', ') As NewCCNumber "
MYSQL = MYSQL & "From COMP As T "
MYSQL = MYSQL & "Group By T.player_id"

quotations: A syntax error has occurred. SQLCODE=-201

I am trying to do something like this:
query = (
"select mandant,posnr,"
"lieferbedingung,nrkreis_nr"
"from eakopf_t where posnr[10,10] = \" \" , and posnr[1,2] not in (\"99\",\"RE\",\"UB\")"
"and mandant <> 999;"
)
queryset += [(query, filename)]
df = pd.read_sql(query, engine.connect())
but I get this error:
SQLNumResultCols failed: [IBM][CLI Driver][IDS/UNIX64] A syntax error has occurred. SQLCODE=-201
I guess its becuase of the quotation marks but i'm not sure how to modify it.
I already tried without backslash too
[SQL: select mandant,posnr,systemdat,fk_kto_auf,fk_kto_not1,fk_kto_not2,fk_kto_empf,fk_kto_adr5,fk_kto_adr6,fk_kto_adr7,fk_kto_adr8,fk_kto_adr9,adr_auftraggeber,anzahl_kolli,volumen,gewicht,lieferbedingung,aufdat,ankdat,versandort,modus,ladehafen,loeschhafen,bereich,nrkreis_nr from eakopf_t where posnr[10,10] = " " , and posnr[1,2] not in ("99","RE","UB") and mandant <> 999;]
You have:
query = (
"select mandant,posnr,"
"lieferbedingung,nrkreis_nr"
"from eakopf_t where posnr[10,10] = \" \" , and posnr[1,2] not in (\"99\",\"RE\",\"UB\")"
"and mandant <> 999;"
)
There is no space separating nrkreis_nr from from, so the printed query would contain:
select mandant,posnr,lieferbedingung,nrkreis_nrfrom eakopf_t where …
It's probably best to put spaces at the start (or the end) of each continuation line:
query = (
"select mandant, posnr,"
" lieferbedingung, nrkreis_nr"
" from eakopf_t where posnr[10,10] = \" \" and posnr[1,2] not in (\"99\",\"RE\",\"UB\")"
" and mandant <> 999;"
)
It's not necessary when there is a punctuation character at the end of one line or the beginning of the next (so the comma at the end of the first line of the string means the space at the start of the next line is not mandatory), but it is safest.
I also removed a stray comma and space in:
"from eakopf_t where posnr[10,10] = \" \" , and posnr[1,2] not in (\"99\",\"RE\",\"UB\")"

How to properly clean column header in Power Query and capitalize first letter only without changing other letter?

I would like to clean a column Header of the table so that my column header that has a name like below:
[Space][Space][Space]First Name[Space][Space]
[Space]MaintActType[Space]
TECO date[Space]
FIN Date
ABC indicator
COGS
Created On
And my desired Column Header Name to be like below:
First Name
Main Act Type
TECO Date
FIN Date
ABC Indicator
COGS
Created On
my code is as below:
let
Source = Excel.Workbook(File.Contents("C:\RawData\sample.xlsx"), null, true),
#"sample_Sheet" = Source{[Item="sample",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(#"sample_Sheet", [PromoteAllScalars=true]),
#"Trim ColumnSpace" = Table.TransformColumnNames(#"Promoted Headers", Text.Trim),
#"Split CapitalLetter" = Table.TransformColumnNames(#"Trim ColumnSpace", each Text.Combine(Splitter.SplitTextByPositions(Text.PositionOfAny(_, {"A".."Z"},2)) (_), " ")),
#"Remove DoubleSpace" = Table.TransformColumnNames(#"Split CapitalLetter", each Replacer.ReplaceText(_, " ", " ")),
#"Capitalise FirstLetter" = Table.TransformColumnNames(#"Remove DoubleSpace", Text.Proper),
#"Remove Space" = Table.TransformColumnNames(#"Capitalise FirstLetter", each Text.Remove(_, {" "})),
#"Separate ColumnName" = Table.TransformColumnNames(#"Remove Space", each Text.Combine(Splitter.SplitTextByCharacterTransition({"a".."z"}, {"A".."Z"}) (_), " "))
in
#"Separate ColumnName"
However, i get the result as below. Which is not what i wanted as all the capital letter we combined together. How do i change the code so that i get the result as wanted? I would really appreciate your help, please.
First Name
Main Act Type
TECODate
FINDate
ABCIndicator
COGS
Created On
Alternatively, i changed the code to:
let
Source = Excel.Workbook(File.Contents("C:\RawData\sample.xlsx"), null, true),
#"sample_Sheet" = Source{[Item="sample",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(#"sample_Sheet", [PromoteAllScalars=true]),
#"Trim ColumnSpace" = Table.TransformColumnNames(Input, Text.Trim),
#"Separate ColumnName" = Table.TransformColumnNames(#"Trim ColumnSpace", each Text.Combine(Splitter.SplitTextByCharacterTransition({"a".."z"}, {"A".."Z"}) (_), " ")),
#"Capitalise FirstLetter" = Table.TransformColumnNames(#"Separate ColumnName", Text.Proper)
in
#"Capitalise FirstLetter"
Unfortunately it return the result like so:
First Name
Main Act Type
Teco Date
Fin Date
Abc Indicator
COGS
Created On
I have no idea how to play around the code anymore.
One way is to mark the existing spaces with something (I used "ZZZ") and restore them back to spaces at the end. Here's your code with a couple of tweaks. Thanks for your code. I was trying to do Text.Proper and your sample helped me!
let
Source = Input,
#"Replaced Value" = Table.ReplaceValue(Source,"[Space]"," ",Replacer.ReplaceText,{"Headers"}),
#"Transposed Table" = Table.Transpose(#"Replaced Value"),
#"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table", [PromoteAllScalars=true]),
#"Trim ColumnSpace" = Table.TransformColumnNames(#"Promoted Headers", Text.Trim),
#"Change space to ZZZ" = Table.TransformColumnNames(#"Trim ColumnSpace", each Replacer.ReplaceText(_, " ", " ZZZ ")),
#"Split CapitalLetter" = Table.TransformColumnNames(#"Change space to ZZZ", each Text.Combine(Splitter.SplitTextByPositions(Text.PositionOfAny(_, {"A".."Z"},2)) (_), " ")),
#"Capitalise FirstLetter" = Table.TransformColumnNames(#"Split CapitalLetter", Text.Proper),
#"Remove Space" = Table.TransformColumnNames(#"Capitalise FirstLetter", each Text.Remove(_, {" "})),
#"Separate ColumnName" = Table.TransformColumnNames(#"Remove Space", each Text.Combine(Splitter.SplitTextByCharacterTransition({"a".."z"}, {"A".."Z"}) (_), " ")),
#"Change ZZZ to space" = Table.TransformColumnNames(#"Separate ColumnName", each Replacer.ReplaceText(_, "ZZZ", " ")),
#"Remove DoubleSpace" = Table.TransformColumnNames(#"Change ZZZ to space", each Replacer.ReplaceText(_, " ", " "))
in
#"Remove DoubleSpace"