Remove part of message string BEFORE and AFTER in logstash config filter - substring

Message: /merlion/dpa2/cn133ta/j4_sryuo/j4_02_sv_ip
I am trying to find a way to remove the strings BEFORE and AFTER a defined string in a message.
In this message, the constant value is dpa2, and I want to discard anything BEFORE dpa2(inclusive), and the "cn133ta/". Only want to retain "j4_sryuo/j4_02_sv_ip".
The position of cn133ta will be of dynamic value, so it is not a constant string. Hope that makes sense.
At the moment, I have tried the gsub below:
mutate { gsub => [ "dir_path", "/[^./]+/tpa2/", "/" ] }
But I'm having trouble finding a way to discard the AFTER string, in this case "cn133ta/".
Thanks

You may use
mutate { gsub => [ "dir_path", "^.*?/dpa2/[^/]+/", "" ] }
See the regex demo
If the dpa2 subpart is right after the first subpart use your approach,
^/[^/]+/dpa2/[^/]+/
^^^^^^
See this regex demo.
Pattern details
^ - start of string
.*? - any 0+ chars other than line break chars, as few as possible
/dpa2/ - a literal string
[^/]+/ - 1+ chars other than / and then a /.

Related

Joi validation with dollar sign in the number text

Is it possible to extend joi to allow for a '$' in the number() validation?
My input is a string like "$12.34". When I attempt to validate this using Joi.number() I receive an error "{Field} must be a number". All I need is to remove the $ and it works fine. Is there any way to do this in the schema definition so that I don't have to scrub my incoming data before calling validate?
const results = Joi.number().validate("$12.34") // fails
const results = Joi.number().validate("12.34") // succeeds
If you are okay with regex then you can use regex to validate the string as follows:
Joi.string().regex(/\$\d+(\.?\d+)?/)
// \$ check for $
// \d+ digit 1 or more
// \.? . one or zero
// (\.?\d+)? match group for 0 or 1 time
As input type is string you can be sure that it contains $ by two ways i.e.
replace $ with empty string.
Joi.number().validate("$12.34".replace("$",""))
splitting string at $ and then checking 2nd part (less secure)
Joi.number().validate("$12.34".split("$")[1])
Last option is to strip of $ every-time and then pass remaining part to check if it's number.
The short answer is to extend number with a custom prepare method.
After trying everything I could think of I looked at the source on github and found this test
it('extends number to support comma delimiter', () => {
const custom = Joi.extend({
type: 'number',
base: Joi.number(),
prepare(value, helpers) {
if (typeof value !== 'string') {
return;
}
return { value: value.replace(',', '.') };
}
});
expect(custom.number().validate(2.0)).to.equal({ value: 2.0 });
expect(custom.number().validate('2.0')).to.equal({ value: 2.0 });
expect(custom.number().validate('2,0')).to.equal({ value: 2.0 });
expect(custom.number().validate('2,0', { convert: false }).error).to.be.an.error('"value" must be a number');
expect(custom.number().validate(undefined).error).to.not.exist();
});
which is basically what I am trying to do so I modified it to fix up '$' instead of ',' and voila
results1 will pass and results2 will error out.

How can you include the delimiter in a .split string in dart?

I have a String as follows:
var _quotedText = "Text1[John;1234]Text2";
Which I want to split into a list as follows:
final List<String> _splitQuotedText = _quotedText.split(RegExp(r"\[([A-z].+);([0-9]+)\]"));
My list comes out as:
['Text1', 'Text2']
But I'd like it to actually comes out as:
['Text1', '[John;1234]Text2']
Meaning the delimiter should be included with the match, and ideally the delimiter would be considered the "start" for a given item.
Is there any kind of straightforward way to tackle this?
A simple way to do this is to use regex's lookahead function.
A lookahead will match whatever is before your lookahead, we can use it to match the space before your delimiter and use that as a delimiter:
A lookahead looks like this:
(?= ... )
So your new regex looks like this:
(?=\[([A-z].+);([0-9]+)\])
Here is your example:
void main() {
var myString = "Text1[John;1234]Text2";
List<String> myList = myString.split(RegExp(r"(?=\[([A-z].+);([0-9]+)\])"));
print(myList); // ['Text1', '[John;1234]Text2']
}

Split a string based on "|" character in PowerShell

I have a string variable in PowerShell which contains the value:
NFP|8dc3b47a-48eb-4696-abe2-48729beb63c8
I am attempting to get the beginning portion of that string into it's own variable by identifying the index of the "|" character and using a substring function to extract the first portion of the string, in this case "NFP". I am not sure how to escape the "|" so I can use it properly. It doesn't seem to recognize it at all. My latest attempt is as follows:
$PolicyManual = $Item["PolicyManual"]
write-host $PolicyManual #Displays NFP|8dc3b47a-48eb-4696-abe2-48729beb63c8
if ($PolicyManual.Contains([regex]::escape("|"))) {
$PolcyManual = $PolicyManual.Substring(0, $PolicyManual.IndexOf([regex]::escape("|")))
}
I'm sure this is simple, but I can't figure out how to make it work. Can anyone offer assistance to a PowerShell novice?
Thanks.
The problem is that .contains method doesn't know about regex and you are never entering the if condition because of this. When you do [regex]::escape("|"), the method is looking for a literal \|.
Try this instead:
$PolicyManual = "NFP|8dc3b47a-48eb-4696-abe2-48729beb63c8"
if ($PolicyManual.Contains('|')) {
$element0, $element1 = $PolicyManual.Split('|')
$element0 #=> NFP
$element1 #=> 8dc3b47a-48eb-4696-abe2-48729beb63c8
}

How to cut a string from the end in UIPATH

I have this string: "C:\Procesos\rrhh\CorteDocumentos\Cortados\10001662-1_20060301_29_1_20190301.pdf" and im trying to get this part : "20190301". The problem is the lenght is not always the same. It would be:
"9001662-1_20060301_4_1_20190301".
I've tried this: item.ToString.Substring(66,8), but it doesn't work sometimes.
What can I do?.
This is a code example of what I said in my comment.
Sub Main()
Dim strFileName As String = ""
Dim di As New DirectoryInfo("C:\Users\Maniac\Desktop\test")
Dim aryFi As FileInfo() = di.GetFiles("*.pdf")
Dim fi As FileInfo
For Each fi In aryFi
Dim arrname() As String
arrname = Split(Path.GetFileNameWithoutExtension(fi.Name), "_")
strFileName = arrname(arrname.Count - 1)
Console.WriteLine(strFileName)
Next
End Sub
You could achieve this using a simple regular expressions, which has the added benefit of including pattern validation.
If you need to get exactly eight numbers from the end of file name (and after an underscore), you can use this pattern:
_(\d{8})\.pdf
And then this VB.NET line:
Regex.Match(fileName, "_(\d{8})\.pdf").Groups(1).Value
It's important to mention that Regex is by default case sensitive, so to prevent from being in a situations where "pdf" is matched and "PDF" is not, the patter can be adjusted like this:
(?i)_(\d{8})\.pdf
You can than use it directly in any expression window:
PS: You should also ensure that System.Text.RegularExpressions reference is in the Imports:
You can achieve it by this way as well :)
Path.GetFileNameWithoutExtension(Str1).Split("_"c).Last
Path.GetFileNameWithoutExtension
Returns the file name of the specified path string without the extension.
so with your String it will return to you - 10001662-1_20060301_29_1_20190301
then Split above String i.e. 10001662-1_20060301_29_1_20190301 based on _ and will return an array of string.
Last
It will return you the last element of an array returned by Split..
Regards..!!
AKsh

Flutter Unicode Apostrophe In String

I'm hoping this is an easy question, and that I'm just not seeing the forest due to all the trees.
I have a string in flutter than came from a REST API that looks like this:
"What\u0027s this?"
The \u is causing a problem.
I can't do a string.replaceAll("\", "\") on it as the single slash means it's looking for a character after it, which is not what I need.
I tried doing a string.replaceAll(String.fromCharCode(0x92), "") to remove it - That didn't work.
I then tried using a regex to remove it like string.replaceAll("/(?:\)/", "") and the same single slash remains.
So, the question is how to remove that single slash, so I can add in a double slash, or replace it with a double slash?
Cheers
Jase
I found the issue. I was looking for hex 92 (0x92) and it should have been decimal 92.
I ended up solving the issue like this...
String removeUnicodeApostrophes(String strInput) {
// First remove the single slash.
String strModified = strInput.replaceAll(String.fromCharCode(92), "");
// Now, we can replace the rest of the unicode with a proper apostrophe.
return strModified.replaceAll("u0027", "\'");
}
When the string is read, I assume what's happening is that it's being interpreted as literal rather than as what it should be (code points) i.e. each character of \0027 is a separate character. You may actually be able to fix this depending on how you access the API - see the dart convert library. If you use utf8.decode on the raw data you may be able to avoid this entire problem.
However, if that's not an option there's an easy enough solution for you.
What's happening when you're writing out your regex or replace is that you're not escaping the backslash, so it's essentially becoming nothing. If you use a double slash, that solve the problem as it escapes the escape character. "\\" => "\".
The other option is to use a raw string like r"\" which ignores the escape character.
Paste this into https://dartpad.dartlang.org:
String withapostraphe = "What\u0027s this?";
String withapostraphe1 = withapostraphe.replaceAll('\u0027', '');
String withapostraphe2 = withapostraphe.replaceAll(String.fromCharCode(0x27), '');
print("Original encoded properly: $withapostraphe");
print("Replaced with nothing: $withapostraphe1");
print("Using char code for ': $withapostraphe2");
String unicodeNotDecoded = "What\\u0027s this?";
String unicodeWithApostraphe = unicodeNotDecoded.replaceAll('\\u0027', '\'');
String unicodeNoApostraphe = unicodeNotDecoded.replaceAll('\\u0027', '');
String unicodeRaw = unicodeNotDecoded.replaceAll(r"\u0027", "'");
print("Data as read with escaped unicode: $unicodeNotDecoded");
print("Data replaced with apostraphe: $unicodeWithApostraphe");
print("Data replaced with nothing: $unicodeNoApostraphe");
print("Data replaced using raw string: $unicodeRaw");
To see the result:
Original encoded properly: What's this?
Replaced with nothing: Whats this?
Using char code for ': Whats this?
Data as read with escaped unicode: What\u0027s this?
Data replaced with apostraphe: What's this?
Data replaced with nothing: Whats this?
Data replaced using raw string: What's this?