Python replace special group of string using regex - c#

Currently my project runs on .net C# with 4.5.0 framework version. Which also integrated to use IronPython for some python script execution.
Now I am looking for replacing some special text from string with other e.g.
PV1|1|??|??^^^??^^^??|||||??^??^??^??^^??^^^??~??^??^??^??^^??^^^??~??^??^??^??^^??^^^??|??^??^??^??^^??^^^??
??~??^??^??^??^^??^^^?~??^??^??^??^^??^^^??~??^??^??^??^^??^^^??~??^??^??^??^^??^^^??~??^??^??^??
??^^??^^^??~??^??^??^??^^??^^^??^??^^??^^^??^??^^??^^^??^^^??^^^??^??^^^??^^^???^??^^^??^??^^^??^^^??^^??^^^??^??^^??^^^??^^^??^^??^^^??|??||||||??^^^.^^^|??||??|||||||||||||||||||??||??|||??||||||
ORC|??||??||||||??|^??^^^??^^^^??
OBR|1|??^??|??^??|??^^^* ??|||??|??||||||||||||||??||??|??|||||||??|
ZDS|??|^TEST^ONLY (TESTONLY)^^^TEST SUPPORT^^^^TEST|??|COMPLETED
OBX|1|??|??^??
from above text I want to replace line for ZDS|??, with OBX|1|?? with rest of the string available in ZDS to be added to OBX. Also want to keep if any OBX string is available in the text as original OBX.
I tried below in Iron Python
import re
regEx = re.compile('^ZDS\|([^|]+)\|([^|]+)\|([^|]+)\|([^|\n\r ]+)')
Message = regEx.sub('OBX|1|FT|||\1~\2~\3~\4|||', Message)
Also
Message = re.sub(r'^ZDS\|([^|]+)\|([^|]+)\|([^|]+)\|([^|\n\r ]+)', 'OBX|1|FT|||\1~\2~\3~\4|||', Message, count=1)
and
Message= str.replace(Message, '^ZDS\|([^|]+)\|([^|]+)\|([^|]+)\|([^|\n\r ]+)','OBX|1|FT|||\1~\2~\3~\4|||')
But all above options did not worked.
I wanted output like
PV1|1|??|??^^^??^^^??|||||??^??^??^??^^??^^^??~??^??^??^??^^??^^^??~??^??^??^??^^??^^^??|??^??^??^??^^??^^^??
??~??^??^??^??^^??^^^?~??^??^??^??^^??^^^??~??^??^??^??^^??^^^??~??^??^??^??^^??^^^??~??^??^??^??
??^^??^^^??~??^??^??^??^^??^^^??^??^^??^^^??^??^^??^^^??^^^??^^^??^??^^^??^^^???^??^^^??^??^^^??^^^??^^??^^^??^??^^??^^^??^^^??^^??^^^??|??||||||??^^^.^^^|??||??|||||||||||||||||||??||??|||??||||||
ORC|??||??||||||??|^??^^^??^^^^??
OBR|1|??^??|??^??|??^^^* ??|||??|??||||||||||||||??||??|??|||||||??|
OBX|1|FT|||^TEST^ONLY (TESTONLY)^^^TEST SUPPORT^^^^TEST~??~COMPLETED
OBX|1|??|??^??

Finally I have got answer to my own question
Original message is
Message =
PV1|1|??|??^^^??^^^??|||||??^??^??^??^^??^^^??~??^??^??^??^^??^^^??~??^??^??^??^^??^^^??|??^??^??^??^^??^^^?? ??~??^??^??^??^^??^^^?~??^??^??^??^^??^^^??~??^??^??^??^^??^^^??~??^??^??^??^^??^^^??~??^??^??^?? ??^^??^^^??~??^??^??^??^^??^^^??^??^^??^^^??^??^^??^^^??^^^??^^^??^??^^^??^^^???^??^^^??^??^^^??^^^??^^??^^^??^??^^??^^^??^^^??^^??^^^??|??||||||??^^^.^^^|??||??|||||||||||||||||||??||??|||??||||||
ORC|??||??||||||??|^??^^^??^^^^??
OBR|1|??^??|??^??|??^^^* ??|||??|??||||||||||||||??||??|??|||||||??|
OBX|1|FT|||^TEST^ONLY (TESTONLY)^^^TEST SUPPORT^^^^TEST~??~COMPLETED
OBX|1|??|??^??
and in Python
import re
Message = re.sub(r'ZDS\|([^|]+)\|([^|]+)\|([^|]+)\|([^|\n\r ]+)', 'OBX|1|FT|||'+r'\1~\2~\3~\4|||', Message, count=1)
Message = re.sub(r'ZDS\|([^|]+)\|([^|]+)\|([^|]+)\|([^|\n\r ]+)', 'OBX|1|FT|||$1~$2~$3~$4|||', Message, count=1)

Related

What's the best way to translate ANTLR errors sentences from english to another language?

The application I'm building targets portuguese speaking users. The users can input a sentence which will be interpreted by ANTLR. To provide the users the best feedback from their input I will need to have errors displayed in portuguese.
I didn't found an configuration or file where i could change the error tokens like in some other libraries.
You cannot change the language of errors/exception coming out of an ANTLR generated parser. You will need to catch these exceptions yourself, and make (for your audience) human readable messages. ANTLR's exceptions have enough for you to do this: line number, column index, tokens: everything is available for you.
As i searched deeper the best alternative is to override the methods on DefaultErrorStrategy and build the message the way you want.
You can set an error Handler to your parser, it should be your new custom error strategy.
typescript DefaultErrorStrategy
Here is my override for reportInputMismatch method with custom translation:
reportInputMismatch(recognizer: Parser, e: InputMismatchException): void {
const expected = e.expectedTokens;
const expectedString = expected ? this.getFriendlyExpectedTokens(expected, recognizer.vocabulary) : '';
const input = this.getTokenErrorDisplay(e.getOffendingToken(recognizer));
const msg = `entrada ${input} não compatível, espera-se ${expectedString}`;
this.notifyErrorListeners(recognizer, msg, e);
}

How to copy and say user's input?

I am making a discord bot and currently trying to make the bot repeat what the user says.
cService.CreateCommand("say")
.Parameter("user", ParameterType.Unparsed)
.Do(async (e) =>
{
Message[] CopiedMessage = await e.Channel.DownloadMessages(1);
await e.Channel.SendMessage(CopiedMessage); //Error, only can print string
});
I am right now having problems trying to print out the CopiedMessage as it's datatype is Message[] and I have to convert to a string to make the bot say it. I tried converting it to a string using ToString but it still doesn't work.
Try defining your CopiedMessage using string CopiedMessage = e.Args[0]; instead.
DownloadMessages() is mainly used for deleting messages on the channel, although it can be used to print out after some complex process. Even if it works , since it is not a string, it will give out a weird string of numbers anyway since you downloaded the message's data, not the message's value.

How to make a echo command in a C# Application?

So, I am making a hacking game, and in it you control a terminal. So far, I have done 'help' and 'cls' commands, that don't require parameters. But, for say a echo command, I need the computer to detect the keyword 'echo' and the rest of the text as what you want to 'echo'. How can I do this? Thanks for any help.
You can do something line this:
var input = Console.ReadLine(); //You might have this already in your code
if (input.StartsWith("echo "))
{
var text = input.Substring(5);
Console.WriteLine(text);
}
This checks to see if the input line starts with echo, and if it does, it prints to the console whatever is after the first 5 characters (which is the length of echo and one space)

printing string in c# using \r\n to start new line

I have a asp.net and c# project.
I need to print this string out, i get it like this from the server:
Working...: 0/0\r\nNavigating: 0/0
It should be printed out in 2 separate lines (using the \r\n), shouldn't it be doing that automatically?
Am i doing something wrong?
I cant change the string but the way i use it, is:
I have a EO progress bar that i send a message like this:
_progressBar.UpdateProgress(_count, progress.Message);
And it displays the message under the progressBar.
The message is the string i posted on top.
Thanks
It seems like your string might contain #"\r\n" instead of "\r\n", case in which you'd have to parse the escape characters yourself.
For example, to replace backslash-r by CR and backslash-n by LF (not the most efficient way to do it):
string s0 = #"A\r\nB"; // s0 would print as: A\r\nB
string s = s0.Replace(#"\r","\r").Replace(#"\n","\n");
Now s contains:
A
B
use <br/> - that is a line break in HTML...
EDIT - as per comment:
string X = #"Working...: 0/0\r\nNavigating: 0/0";
string Y = X.Replace ( #"\r\n", #"<br/>" ); // Result is Working...: 0/0<br/>Navigating: 0/0
The server has to send to the client data that the latter understands. JSON, HTML, XML or whatever data type you're working with.
From the client's point of view \r\n is just a string like any other. In this case you may want to send something like:
Working...: 0/0<br/>Navigating: 0/0
And print it has HTML.
You probably are sending a backslash followed by an n or r and not a \r or \n.
If so you have to parse them yourself or get the output to be \r and \n
Yes it is automatic.
using System;
public class Test
{
public static void Main(string[] args)
{
string mesg = "Working...: 0/0\r\nNavigating: 0/0";
Console.WriteLine(mesg);
}
}
C:\temp>csc Test.cs
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.
Copyright (C) Microsoft Corporation. All rights reserved
C:\temp>Test
Working...: 0/0
Navigating: 0/0
C:\temp

C# - MessageBox - Message localization in resources and lines breaks

I would like to show MessageBox (WinForms) with string from Resources with lines breaks.
example without Resources (WORKS):
string someMsg = "Message. Details:\n" + someDetails;
MessageBox.Show(someMsg);
Result:
Message. Details:
here are some details
When I move string "Message. Details:\n" into Resources:
string someMsg = GlobalStrings.MsgBoxJustTest + someDetails;
MessageBox.Show(someMsg);
Result:
Message. Details:\nhere are some details
When I moved string with "\n" to resources then MessageBox.Show() stopped to interpret it as newline.
Edit: I'm thinking about: someMsg.Replace(#'\n',Environment.NewLine);
but it's still quite annoying for so simple thing.
if you add that to resources it doesn't take \n as escape charecter
Just open your resource file in notepad to see this and cahnge in XML file(resx)
or
Type your data in notepad with new line.
Copy that and paste in your resource editor
edit:
or
Type/Paste your data into the resource editor UI, select the \n and replace it with an actual linebreak, with Shift-Enter.
You could do something like this (as long as your not .net 2.0):
public static class StringExt
{
public static String FixNewLines(this String str)
{
return str.Replace(#'\n',Environment.NewLine);
}
}
And then:
string someMsg = GlobalStrings.MsgBoxJustTest + someDetails;
MessageBox.Show(someMsg.FixNewLines());
However, this will affect ALL strings in your application (namespace scope)
It's a dirty fix, but it's a quick fix.
Personally, I would just fix my logic all the way through, rather than do something like the above.
Maybe you can Open the resx file as code and add the line breaks directly in the
XML
OR
Possibly they get lost when reading due to escape character maybe try using \\
One easy solution is to store “placeholders” in a resource strings. For instane, this string is stored in *.resx under “MessageDetails” key: "Message. Details:{0}{1}". Then, in your code, use it like this:
MessageBox.Show(String.Format(GlobalStrings.MessageDetails, Environment.NewLine, #"The message"));
The advantage here is a portability, as you can see.

Categories

Resources