Error System.BadImageFormatException, Couldn't not load assembly, using C# - c#

Hello i'm having difficulty because i can't load my assembly. I can't because when i join them together and after split the assemblies using a DELIMITER the image of my files are corrupted. The result is well and i can see perfectly the same assembly, however when i do the split, my file will return me System.BadImageFormatException.
Here is the program that i did for joining the files:
using System;
using System.Text;
using System.IO;
using System.Collections.Generic;
namespace Crypter
{
class Program
{
[STAThread]
static void Main(string[] args)
{
//No Arguments -> Exit
if (args.Length < 2)
{
Console.WriteLine("Syntax: crypter.exe <Exe/Dll to get Encrypted> <Password> (Optional: output file name)");
Environment.Exit(0);
}
String file = args[0];
String pass = args[1];
String outFile = "Crypted.exe";
//If Output Name is specified -> Set it
if (args.Length == 3)
{
outFile = args[2];
}
//File doesn't exist -> Exit
if (!File.Exists(file))
{
Console.WriteLine("[!] The selected File doesn't exist!");
Environment.Exit(0);
}
//Everything seems fine -> Reading bytes
Console.WriteLine("[*] Reading Data...");
byte[] plainBytes = File.ReadAllBytes(file);
//Yep, got bytes -> Encoding
Console.WriteLine("[*] Encoding Data...");
byte[] encodedBytes = encodeBytes(plainBytes, pass);
Console.WriteLine("[*] Save to Output File... ");
//Leer el stub
Console.WriteLine("[*] Reading Stub...");
byte[] Stub = File.ReadAllBytes("Stub.exe");
//byte separador
string strseperate = "BLAUMOLAMUCHO";
byte[] toBytes = Encoding.ASCII.GetBytes(strseperate);
//byte[] toBytes = new byte[30];
//write bytes
//var stream
//Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("skip.skip.exe");
//Console.WriteLine(stream);
var s = new MemoryStream();
s.Write(Stub, 0, Stub.Length);
s.Write(toBytes, 0, toBytes.Length);
s.Write(encodedBytes, 0, encodedBytes.Length);
var b3 = s.ToArray();
Stream stream = new MemoryStream(b3);
//Stream stream = new MemoryStream(encodedBytes);
FileStream fileStream = new FileStream(#"out.exe", FileMode.Create, FileAccess.Write);
for (int i = 0; i < stream.Length; i++)
fileStream.WriteByte((byte)stream.ReadByte());
Console.WriteLine("Done!");
Console.WriteLine("\n[*] File successfully encoded!");
}
private static byte[] encodeBytes(byte[] bytes, String pass)
{
byte[] XorBytes = Encoding.Unicode.GetBytes(pass);
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] ^= XorBytes[i % XorBytes.Length];
}
return bytes;
}
}
}
And then i open since the first program, stub.. the program is:
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Resources;
using System.Security.Cryptography;
using System.Reflection;
using Microsoft.Win32;
namespace skip
{
static class Program
{
/// <summary>
/// MAIN
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1());
//leemos el byte array
byte[] file = File.ReadAllBytes(System.Reflection.Assembly.GetExecutingAssembly().Location);
//obtenemos la string
string str = System.Text.Encoding.ASCII.GetString(file);
string[] arr=str.Split(new string[] { "BLAUMOLAMUCHO" }, StringSplitOptions.None);
string a = arr[0];
string b = arr[1];
/*Console.WriteLine(a);
Console.WriteLine("------------------------------");
Console.WriteLine(b);
Console.ReadKey();*/
byte[] encodedBytes = Encoding.ASCII.GetBytes(b);
RunInternal(encodedBytes,"1234");
}
private static void RunInternal(byte[] exeName, String pass)
{
//Read the raw bytes of the file
byte[] resourcesBuffer = exeName;
//Decrypt bytes from payload
byte[] decryptedBuffer = null;
decryptedBuffer = decryptBytes(resourcesBuffer, pass);
//If .NET executable -> Run
if (System.Text.Encoding.ASCII.GetString(decryptedBuffer).Contains("</assembly>")) //Esto devuelve false
{
//Load the bytes as an assembly
Assembly exeAssembly = Assembly.Load(decryptedBuffer);
//Execute the assembly
object[] parameters = new object[1]; //Don't know why but fixes TargetParameterCountException
try{
exeAssembly.EntryPoint.Invoke(null, parameters);
}catch (Exception ex){
Console.WriteLine(ex);
Console.ReadKey();
}
}
else
{
Console.WriteLine(Encoding.ASCII.GetString(decryptedBuffer));
Console.ReadKey();
}
}
/// <summary>
/// Decrypt the Loaded Assembly Bytes
/// </summary>
/// <param name="payload"></param>
/// <returns>Decrypted Bytes</returns>
private static byte[] decryptBytes(byte[] bytes, String pass)
{
byte[] XorBytes = Encoding.Unicode.GetBytes(pass);
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] ^= XorBytes[i % XorBytes.Length];
}
return bytes;
}
}
}
I know that it's possible because i did in other languages like autoit. And my binary add perflectly the delimiter that i can see when i convert to string:
MZ? ♥ ♦ ?? ? # ? ♫▼?♫ ? ?!?☺L?!T
his program cannot be run in DOS mode.
$ PE L☺♥ ?4‼Y ? ☻☺♂☺♂ ♫ N, # # ☻ ♦ ♦
? ☻ ♥ #? ► ► ► ► ► ?+ S # ♣
` ♀ ?* ∟ H
.text T♀ ♫ ☻ `.rsrc ♣ # ♠ ►
# #.reloc ♀ ` ☻ ▬ # B 0, H ☻ ♣ ?
! $ ☺ ☺ ♠ ‼0☻ # ☺ ◄ (
↕
▬(‼
(¶
o§
(▬
▼→(↨
♂(↑
♠o↓
(→
(←
&*←0♥ ? ☻ ◄ ☻(∟
‼♣◄♣-♣8? ☻(▬
¶♂♠♥(♥ ♠♂(↑
o↓
r☺ po↔
▬?☺‼♣◄♣-9 (▲
o▼ ☺
¶ o
& ?¶‼♦ ◄♦(!
(←
& ? +↓ (↑
o↓
(→
(←
& *☺► P ↕b ¶▼ ☺‼0♣ B ♥ ◄ ("
♥o#
-?☻♀+* BSJB☺ ☺ ♀ v4.0.30319 ♣ l ?☻ #~ H♥ (♦ #Strings
p ∟ #US ? ► #GUID ? ?☺ #Blob ☻ ☺G§☻ ?%3 ▬ ☺ ☻ ♥
♦ # ☼ ♥ ☺ ☻
☺ ♠ 0 ) ♠ ? h ♠ ? ? ♠ ? ? ♠ ? ? ♠ ♠☺? ♠ ▼☺? ♠ 8☺? ♠ S☺? ♠ n☺? ♠ ?☺?☺♠ ?☺?☺♠
?☺? ♠ ?☺? ♠ ◄☻?☺? %☻ ♠ T☻4☻♠ t☻4☻♠ ?☻)
?☻?☻♠ ?☻? ♠ 0♥&♥♠ B♥) _ N♥ ♠ v♥j♥♠ ?♥) ♠ ?♥) ♠ ?♥) ♠ ?♥? ♠ ?♥? ♠ ♦♦) ♠ #♦)
☺ ☺ ☺ ?☺► ‼ ← ♣ ☺ ☺ P ? 7
☺ ? ? < ♫ ☺ L! ? H ¶ ♥ ☺ U ☻ ] ☺ b ☻ ] ◄ ? ∟ ↓ ? ∟ ! ? ∟ ) ? ∟ 1
? ∟ 9 ? ∟ A ? ∟ I ? ∟ Q ? ∟ Y ? ! a ? ∟ i ? ∟ q ? ∟ y ? & ? ? , ? ? 1 ? ? 1 ? ?
☻
? ?☻: ? ♦♥? ? ↓♥D ? 5♥H ? \♥N ? ⌂♥T ? ?♥Y ? ?♥_ ? ?♥d ? ?♥p ? ?♥u ? ?♥z ? ?♥? ?
?♥? ? ?♥? ? ♫♦T ? →♦? ? 5 . ; ☺. ‼ ? . ← ☺☺. # ☺☺. + ☺☺. 3 ? . ♂ ? . C ☺☺. S
☺☺. [ ▼☺. k I☺. s V☺. { _☺. ? h☺i ? ? ♦? ☺ ← ♦ ☺
♦ ☺ ?☻ <Module> skip.exe Program skip mscorlib System Object M
ain RunInternal decryptBytes exeName pass bytes System.Runtime.Versioning Target
FrameworkAttribute .ctor System.Reflection AssemblyTitleAttribute AssemblyDescri
ptionAttribute AssemblyConfigurationAttribute AssemblyCompanyAttribute AssemblyP
roductAttribute AssemblyCopyrightAttribute AssemblyTrademarkAttribute AssemblyCu
ltureAttribute System.Runtime.InteropServices ComVisibleAttribute GuidAttribute
AssemblyVersionAttribute AssemblyFileVersionAttribute System.Diagnostics Debugga
bleAttribute DebuggingModes System.Runtime.CompilerServices CompilationRelaxatio
nsAttribute RuntimeCompatibilityAttribute STAThreadAttribute System.Windows.Form
s Application EnableVisualStyles SetCompatibleTextRenderingDefault Assembly GetE
xecutingAssembly get_Location System.IO File ReadAllBytes Environment SpecialFol
der GetFolderPath System.Text Encoding get_ASCII GetString Console WriteLine Con
soleKeyInfo ReadKey Exists String Contains Load MethodInfo get_EntryPoint Method
Base Invoke Exception get_Unicode GetBytes Byte ↨< / a s s e m b l y > ????
=??J?▲??R???z\V↓4??♥ ☺♣ ☻☺♫♫ ☻↔♣↔♣♫♦ ☺☺♫♦ ☺☺☻♣ ☺☺◄A♦ ☺♥ ☺♦☺ ♦ ☺☺☻♦ ↕U♥ ♫♣
♠↔♣↔♣↕U↔∟↕}☻♣ ☺↔♦↔♣☻G☺ →.NETFramework,Version=v4.0☺ T♫¶FrameworkDisplayName►.NET
Framework 4 ☺ ♦Stub ♣☺ ↨☺ ↕Copyright ?? 2017 )☺ $0f86beb5-80bf-47f1-89
e1-df778adbda88 ♀☺ 1.0.0.0☺ ☺ ▲☺ ☺ T☻▬WrapNonExceptionThrows☺ ?4‼Y
☻ ∟☺ ?* ?♀ RSDS§2??♦YL?{Q‼????☺ c:\Users\Androide\Desktop\CRYPTER SIN DR
OPEAR\Stub\obj\Debug\skip.pdb
, >,
0, _CorExeMain mscoree.dll ?% #
☻ ► ?↑ 8 ? ☺ ☺ P ?
☺ ☺ h ? ☺ ? ☺ ? ?# ?☻
0C ?☺ ?☻4 V S _ V E R S I O N _ I N F O ?♦?? ☺ ☺ ☺
? ♦ ☺ D ☺ V a r F i l e I n f o $ ♦ T r a n s
l a t i o n ?♦?☺ ☺ S t r i n g F i l e I n f o ?☺ ☺ 0 0 0 0 0 4 b 0
4 ♣ ☺ F i l e D e s c r i p t i o n S t u b ☺ F i l e V e r s i o n
1 . 0 . 0 . 0 4 ☺ I n t e r n a l N a m e s k i p . e x e H ↕ ☺ L
e g a l C o p y r i g h t C o p y r i g h t ? 2 0 1 7 < ☺ O r i
g i n a l F i l e n a m e s k i p . e x e , ♣ ☺ P r o d u c t N a m e
S t u b ☺ P r o d u c t V e r s i o n 1 . 0 . 0 . 0 8 ☺ A s s e m b l
y V e r s i o n 1 . 0 . 0 . 0 ???<?xml version="1.0" encoding="UTF-8" stan
dalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="asInvoker" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
♀ P<
BLAUMOLAMUCHO|Z? 0 4 5 2 ??4 ? 2 3 4 q 2 3 4 1 2 3 4 1 2 3
94 1 2 3 4 1 2 ? 4 ?▼?♫3?=?►?3L?!`hXs↕pAoSrPm↕cRnZoE Pe‼rAn◄i\ wOg \oVe↔
§ 2 3 4 aE2 ⌂☺7 ??#Y3 4 1 2 ? 6☺:☺☻ 42 3 4 ?'2 3 4 1#2 3 t 1 2 3☻4 5 2 3 4 5 2 3
4 1?2 3☻4 1 2 0 t?1 " 3►4 1 " 3►4 1 2 # 4 1 2 3 4 Q'2 | 4 1#2 ?♣4 1 2 3 4 1 2 3
4 1`2 ? 4 ↓&2 / 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 ; 4 1 2 3 4 9 2 {
4 1 2 3 4 ▼tWxG 4 ?2 3 42 3☻4 1 2 3 4 1 2 ‼ 4`▼rArP 4 ?♣2 3#4 1♠2 3
4 1 2 3 4 1 2 s 4#▼rWl\c4 = 2 3`4 1☻2 3►4 1 2 3 4 1 2 s 4B1 2 3 4 1 2 3 4 ?'2 3
4 y 2 1 1 M 2 ?♣4 2 2 2 4♠1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 O #☺3
D(< 2
3r/ 1p→♫3 > &(= 3
↕*‼☻→►3 > ← 2 qS~B0 3 3 4 = 2 E4→0▼3☻3☻94 1 7 _ 4 ?☺2 ►~4 §☻2 w☻4 ↕SFrZnSs1 2 [♦
4 Q 2 ►Ug ?♦2 # 4 ↕GgIw 4 ?♦2 ? 4 ↕B^oQ 4 1 2 1 4☺v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b b m e q c a z { ⌂ m z z w } 3 4 ?♦??3 5 1 3 ‼W?↑1 3 ‼W?↑♫ 2 3 4 5 2 2
4 1 2 3 4 1 2 w 4 0 d R F w [ _ Q x \ U [ 1 2 ↨ 0 1 f A U _ A _ U E [ \ Z 1 2 3
?♦?☻2 2 g E # Z Z V t Z X T { ] R ^ 2 A☻4 0 ☻ ♥ ♦ ☺ ☻ V ☺ 2 ) 5 0 q \ Y \ W ]
# B 2 3 4 ‼ 3 2 w ^ _ C U _ K } U \ W 3 4 1 2 ♂ < 0 t Z X T v V G R # Z D E [ \
; 0 t Z X T d V F B [ \ Z 1 2 ☻ → ☺ ∟ ♣ ☻
↔ ♠ ♥ ☺ ♥ 1 2 ♂ 8 0 { ] # T # ] U ] | R Y T 2 p [ _ A \ X T ∟ V L T 2 q ; 0 ~
V S P ^ p [ A K A ] V Z G 4 r ] C M C [ T \ E ↕ ☺ ♦ ♣ 3 4 ← 3 2 x T U R X e #
R P T _ R F Z A 3 4 1 2 s 8 0 } A ] V [ ] U ] t Z X T \ R Y T 2 p [ _ A \ X T ∟
V L T 2 ♥ < 0 b A [ U G P # ⌂ S ^ Q 1 2 p [ _ A \ X T 2 q ; 0 b A [ U G P # g W
A G X ] ] 4 ∟ ♥ → ☺ ♀ ▼ ☺ ☺ ♠ 3 4 w = 2 u B A V Y S ^ J ¶ g W A G X ] ]
4 ∟ ♥ → ☺ ♀ ▼ ☺ ☺ ♠ 3 4 ?C2 ?☺4 1 2 3 4 ???<♀xYl◄vWr#i[n♀"♥.♥"¶e_c]dZnS
=‼UfF▲8▬ BtSnWaXo_e☼"JeG"♫>?
>
aBsWmQlM Im^n#=▬uCsPhQmPs▼mZcFoBoTt▲c[m♂aAm↔v♣"◄mSnZfQsEVWr#i[n♀"♥.♥"
8 ‼ ¶ ◄<#eBuQsEeVPAiBi]eUe# Lm]nA=◄uFn♂sQhVmUs∟m[cAoGoWt▼c\m♫aBm∟v "
; ↕ ‼ ¶ ◄<#eBuQsEeVEKeWuEi]n⌂eBe] ^eEeX=‼aAI]v[kTr► FiucReAs♫"Ra]sW"∟>9
◄ ↕ ‼/CeCuVs#eUP#iEiXeVeA>>
><▲aAsVmVlH>2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 ?
4 ?72 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3
4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3
4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3
4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3
4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3
4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3
4 1 2 3 4 1 2 3 4 1 2 3 4
Error return's me:
If you search in the assembly you will see perflectly the DELIMITER, BLAUMOLAMUCHO. I tried including the length's but i must to use streamreader because it's not data.
How i can load two assemblies without using like resource in c#(it's only for educational purposes and learning.)

Related

Regular expression to include starting string

I have managed to match into groups as follows using the below expression but its incomplete.
\([^\)]*\)
Example strings are,
s11(h 1 1 c)(h 1 1 c) x="" y="" z="" phi="" theta=""
e(45,10,h 1 1 c,1,cross,max) x="" y="" z="" phi="" theta=""
With the above expression I can match (h 1 1 c)(h 1 1 c) and (45,10,h 1 1 c,1,cross,max)
But I want to capture the starting string s11 and e along with (h 1 1 c)(h 1 1 c) and (45,10,h 1 1 c,1,cross,max)
You can use
var lines = new List<string> { "s11(h 1 1 c)(h 1 1 c) x=\"\" y=\"\" z=\"\" phi=\"\" theta=\"\"",
"e(45,10,h 1 1 c,1,cross,max) x=\"\" y=\"\" z=\"\" phi=\"\" theta=\"\""};
foreach (var s in lines)
{
Console.WriteLine("==== Next string: \"" + s + "\" =>");
Console.WriteLine(string.Join(", ",
Regex.Matches(s, #"\w+(?:\([^()]*\))+").Cast<Match>().Select(x => x.Value)));
Console.WriteLine("=== With groups and captures:");
var results = Regex.Matches(s, #"(\w+)(?:(\([^()]*\)))+");
foreach (Match m in results)
{
Console.WriteLine(m.Groups[1].Value);
Console.WriteLine(string.Join(", ", m.Groups[2].Captures.Cast<Capture>().Select(z => z.Value)));
}
}
See the C# demo. Output:
==== Next string: "s11(h 1 1 c)(h 1 1 c) x="" y="" z="" phi="" theta=""" =>
s11(h 1 1 c)(h 1 1 c)
=== With groups and captures:
s11
(h 1 1 c), (h 1 1 c)
==== Next string: "e(45,10,h 1 1 c,1,cross,max) x="" y="" z="" phi="" theta=""" =>
e(45,10,h 1 1 c,1,cross,max)
=== With groups and captures:
e
(45,10,h 1 1 c,1,cross,max)
Depending on what exact results you want to get, you may use a regex with or without capturing groups:
\w+(?:\([^()]*\))+
(\w+)(?:(\([^()]*\)))+
See the regex 1 demo and regex 2 demo.
Details
\w+ - one or more word chars (letters, digits and some connector puncutation)
(?:\([^()]*\))+ - one or more repetitions of
\( - a ( char
[^()]* - zero or more chars other than ( and )
\) - a ) char.

how to remove delimiters in C#

Hi I have tried already the code below and it does not remove the delimeters in a dat file:
StreamReader input = new StreamReader(txtFile.Text);
string content = input.ReadToEnd().Trim();
string[] split = System.Text.RegularExpressions.Regex.Split(content, "\\s+", RegexOptions.None);
foreach (string s in split)
{
txtStatus.Text = s + "\r\n" + txtStatus.Text;
}
here is a sample data from the dat file I am working on:
xú !' Date D Time C Millitm N TagIndex N Value C Status C! Marker C" Internal C#
2020032403:25:25829 0 # Bÿÿÿÿ 2020032403:25:25829 1 9# Bÿÿÿÿ 2020032403:25:26844 0 # 2020032403:25:26844 1 9# 2020032403:25:27845 0 # 2020032403:25:27845 1 :# 2020032403:25:28847 0 # 2020032403:25:28847 1 ;# 2020032403:25:29851 0 # 2020032403:25:29851 1 <# 2020032403:25:30857 0 # 2020032403:25:30857 1 =# 2020032403:25:31861 0 #
2020032403:25:31861 1 ># 2020032403:25:32867 0 # 2020032403:25:32867 1 ?#
2020032403:25:33873 0 # 2020032403:25:33873 1 ## 2020032403:25:34877 0 # 2020032403:25:34877 1 €## 2020032403:25:35879 0 # 2020032403:25:35879 1 A# 2020032403:25:36888 0 # 2020032403:25:36888 1 €A# 2020032403:25:37890 0 # 2020032403:25:37890 1 B# 2020032403:25:38838 0 # 2020032403:25:38838 1 €B# 2020032403:25:39841 0 # 2020032403:25:39841 1 C# 2020032403:25:40846 0 # 2020032403:25:40846 1 €C# 2020032403:25:41849 0 # 2020032403:25:41849 1 D# 2020032403:25:42851 0 # 2020032403:25:42851 1 €D# ! 2020032403:25:43852 0 # " 2020032403:25:43852 1 E# # 2020032403:25:44860 0 # $ 2020032403:25:44860 1 €E# % 2020032403:25:45862 0 # & 2020032403:25:45862 1 F# ' 2020032403:25:46869 0 # ( 2020032403:25:46869 1 €F# ) 2020032403:25:47873 0 # * 2020032403:25:47873 1 G# + 2020032403:25:48883 0 # , 2020032403:25:48883 1 €G# - 2020032403:25:49887 0 # . 2020032403:25:49887 1 H# / 2020032403:25:50842 0 # 0 2020032403:25:50842 1 €H# 1 2020032403:25:51844 0 # 2 2020032403:25:51844 1 I# 3 2020032403:25:52866 0 # 4 2020032403:25:52866 1 €I# 5 2020032403:25:53868 0 # 6 2020032403:25:53868 1 J# 7 2020032403:25:54884 0 # 8 2020032403:25:54884 1 €J# 9 2020032403:25:55886 0 # : 2020032403:25:55886 1 K# ; 2020032403:25:56896 0 # < 2020032403:25:56896 1 €K# = 2020032403:25:57860 0 # > 2020032403:25:57860 1 L# ? 2020032403:25:58866 0 # # 2020032403:25:58866 1 €L# A 2020032403:25:59868 0 # B 2020032403:25:59868 1 M# C 2020032403:26:00873
can anyone help me?
It depends on your definition of special characters. To remove all characters except numbers, alphabets, spaces and ":" you can use following pattern [^0-9a-zA-Z:\s]+
s = Regex.Replace(s, "[^0-9a-zA-Z]+", "");
txtStatus.Text = s + "\r\n" + txtStatus.Text;

To Count Occurrences of all sub strings in string C#

Question: I have a long string and I require to find the count of occurrences of all sub strings present under that string and print a list of all sub strings and their count (if count is > 1) in decreasing order of count.
Example:
String = "abcdabcd"
Result:
Substrings Count
abcd 2
abc 2
bcd 2
ab 2
bc 2
cd 2
a 2
b 2
c 2
d 2
Problem: My string can be 5000 character long and I am not able to find a efficient way to achieve this.( Efficiency is very important for application)
Is there any algorithm present or by multi threading it is possible. please help.
Example using: Find a common string within a list of strings
void Main()
{
"abcdabcd".getAllSubstrings()
.AsParallel()
.GroupBy(x => x)
.Select(g => new {g.Key, count=g.Count()})
.Dump();
}
// Define other methods and classes here
public static class Ext
{
public static IEnumerable<string> getAllSubstrings(this string word)
{
return from charIndex1 in Enumerable.Range(0, word.Length)
from charIndex2 in Enumerable.Range(0, word.Length - charIndex1 + 1)
where charIndex2 > 0
select word.Substring(charIndex1, charIndex2);
}
}
Produces:
a 2
dabc 1
abcdabc 1
b 2
abc 2
dabcd 1
bc 2
bcda 1
abcd 2
ab 2
bcdab 1
cdabc 1
abcda 1
d 2
bcdabc 1
dab 1
bcd 2
abcdab 1
c 2
bcdabcd 1
abcdabcd 1
cd 2
da 1
cdab 1
cda 1
cdabcd 1

how to select only status using regular expression

Hi coders I have a string of statement where I have to fetch only status in it
TKT-2297433475972 RCI- 1A LOC-696L9U
OD-NYCNYC SI- FCMI-K POI-DCA DOI-02JUN14 IOI-49881134
1.SINGH/BALJINDER ADT ST
1 OJFK KU 102 C 03JUN2145 OK CLRTUS5 F 2PC // Want this F
2 XKWI KU 381 C 04JUN2230 OK CLRTUS5 F 2PC // Want this F
3 ODEL KU 382 C 20JUN0555 OK CLRTUS5 O 2PC // Want this 0
4 XKWI KU 117 C 20JUN0905 OK CLRTUS5 I 08JUN 2PC // Want this I
JFK
FARE I IT // Don't want this I
TOTALTAX USD 462.04
TOTAL IT
/FC NYC KU X/KWI KU DEL M/BT CLRTUS5 KU X/KWI KU NYC M/BT CLRTUS
5 END XT5.50YC17.50US17.50US5.00XA7.00XY2.50AY1.60YX25.08IN11.51
YM20.70IN3.65WO4.50XF JFK4.5
TKT-0017434068283-284 RCI- 1A LOC-69DQPQ
OD-AUSAUS SI- FCMI-I POI-DCA DOI-03JUN14
1.MIMBELA/JAIME JOSE ADT ST
1 OAUS AA 290 M 02JUL1005 OK MHXE1NA/CONS O 02JUL02JUL 1PC // Want this 0
2 XJFK AA 66 M 02JUL1730 OK MHXE1NA/CONS O 02JUL02JUL 1PC // Want this 0
3 BCN ARNK
4 OFCO AA 111 L 29JUL1115 OK LHXETNI4/CONS O 29JUL29JUL 1PC // Want this 0
5 XORD AA1398 L 29JUL2040 OK LHXETNI4/CONS O 29JUL29JUL 1PC // Want this 0
AUS
FARE I IT// Don't want this I
TOTALTAX USD 639.21
TOTAL IT
in the above string i have to fetch only ST
like this for first
F
F
O
I
At present i m using this regex (?:\s[A-Z]{1}\s\s)
by using this regex it also give me value of I in front of FARE which i don't want how can i get only ST but not I present in after FARE.
Any help will be appreciated
You know, we are just guessing, but what's with that:
^\d .+?\W{2}([A-Z]) \W{2}
Get the second matching group for that
Here a visual explanation: https://www.debuggex.com/r/kAjngGwRrsHQARYE
To be precise: This works with Ignore Case but which is more important: The Multi line switch must be set so that ^ matches the beginning of a line.
Here is C# Code for you
var foo = "1 OJFK KU 102 C 03JUN2145 OK CLRTUS5 F 2PC";
var r = new Regex(#"^\d .+?\W{2}([A-Z]) \W{2}", RegexOptions.IgnoreCase | RegexOptions.Multiline);
var m = r.Matches(foo);
Console.WriteLine(m[0].Groups[1]);
Hi samuel and anurag thanks for your help with regex of anurag and idea of samuel i made some change and get the out what i desire. This is my code:
string foo = #"
TKT-2207434010779-780 RCI- 1A LOC-68XMG5
OD-HOUHOU SI- FCMI-F POI-NYC DOI-02JUN14 IOI-10729143
1.WAHBA/REDA ADT ST
1 OIAH LH7620UA L 07JUN2050 OK LHHNC5N/CN05 F 07JUN07JUN 1PC
2 XLHR LH 911 L 08JUN1645 OK LHHNC5N/CN05 F 08JUN08JUN 1PC
3 XFRA LH 584 L 08JUN2130 OK LHHNC5N/CN05 F 08JUN08JUN 1PC
4 OCAI LH 585 L 06JUL0340 OK LHHNC5N/CN05 O 06JUL06JUL 1PC
5 XFRA LH 440 L 06JUL1000 OK LHHNC5N/CN05 O 06JUL06JUL 1PC
IAH
FARE F USD 522.00
TOTALTAX USD 656.80
TOTAL USD 1178.8
";
StringReader reader = new StringReader(foo);
string line;
List<string> list = new List<string>();
while (null != (line = reader.ReadLine()))
{
Regex r = new Regex(#"^\s\d.+?\W{1}([A-Z]) \W{2}", RegexOptions.Multiline);
if(r.IsMatch(line)){
MatchCollection m = r.Matches(line);
string value = m[0].Groups[1].ToString();
list.Add(value);
}
}
^\d+\s+\w+\s+\w+\s+\w+\s+\w \w+ \w+ \w+\s+(?<YourVar>\w+)
This would work. but it is hard to create a reg expression with just one sample. When using the regex above, every match should contain a group called "YourVar" which contains the desired value
I checked follwing in my code and it is working.
Regex.Match(s, #"\W{2}([A-Z])\W{2}").Value.Trim()

Linked rows, single table SQL

I have a table with parts and different phases. FromID=0 is where the material for the parts is entered. part1 and part2 are processed and then assembled to part3.
ID Partname FromID Parts Process
----------------------------------------------------
1 part1 0 10 process1 part1
2 part1 1 0 process2 part1
3 part2 0 10 process1 part2
4 part2 1 0 process2 part2
5 part3 2 0 Assembly part1
6 part3 4 0 Assembly part2
Is there a way to write a query to get a part and all its subparts that can handle nested subparts as well? I'm using MS Access 2010 and C#.
The result should look like this
PartName = part1
ID Partname FromID Parts Work
-------------------------------------------
1 part1 0 10 work1 part1
2 part1 1 0 work2 part1
PartName = part2
ID Partname FromID Parts Work
-------------------------------------------
3 part2 0 10 work1 part2
4 part2 1 0 work2 part2
PartName = part3
ID Partname FromID Parts Work
-------------------------------------------
1 part1 0 10 work1 part1
2 part1 1 0 work2 part1
5 part3 2 0 Assembly part1
3 part2 0 10 work1 part2
4 part2 1 0 work2 part2
6 part3 4 0 Assembly part2
Use modified pre order tree traversal to mark the rows for quick querying. Check out this write up:
http://iamcam.wordpress.com/2006/03/24/storing-hierarchical-data-in-a-database-part-2a-modified-preorder-tree-traversal/
If you are planning to deploy on SQL server you could consider using common table expressions:
http://msdn.microsoft.com/en-us/library/ms190766(v=sql.105).aspx

Categories

Resources