Regex for Masking Data in a JSON Array - arrays

I've got a requirement to mask the incoming JSON request inside an array using regular expression on Splunk indexer. The JSON data looks like this:
{"Name":["Jobs","Bill"]}
I'm expected to mask the incoming data so that it looks like this:
{"Name":["******","******"]}
And the regex I'm using to mask the data looks something like this:
s/\"Name\":\"[^"]*\"/"Name":"******"/g
But for some reason I'm unable to mask the JSON data. Could any of you good folks please help?

You can use
s/(?:\G(?!^)\",|\"Name\":\[)\"\K[^\"]*/******/g
To support escaped \", use
s/(?:\G(?!^)\",|\"Name\":\[)\"\K[^\"\\]*(?:\\.[^\"\\]*)*/******/g
See the regex demo #1 and regex demo #2
Details
(?:\G(?!^)\",|\"Name\":\[) - either the end of the previous match and then ", substring, or "Name":[ substring
\" - " char
\K - match reset operator discarding all text matched so far
[^\"]* - any zero or more chars other than ".
[^\"\\]*(?:\\.[^\"\\]*)* - any 0+ chars other than " and \ and then zero or more repetitions of a \ followed with any char but a line break char and then any 0+ chars other than \ and ".

Related

Extract particular text from String in Snowflake

I m new to snowflake.
Input String : ["http://info.wealthenhancement.com/ppc-rt-retirement-planning"]
Output String : info.wealthenhancement.com/ppc-rt-retirement-planning
Please help to get output string.
Thanks
Use the substr function to only take characters from the 8th character to the end:
select
'http://info.wealthenhancement.com/ppc-rt-retirement-planning' as orig_value,
substr(orig_value, 8) as new_value
The output is:
+-------------------------------------------------------------+-------------------------------------------------------+
|ORIG_VALUE | NEW_VALUE |
+-------------------------------------------------------------+-------------------------------------------------------+
|http://info.wealthenhancement.com/ppc-rt-retirement-planning | info.wealthenhancement.com/ppc-rt-retirement-planning |
+-------------------------------------------------------------+-------------------------------------------------------+
This will work for http and https URLs by splitting using // as a delimiter. Only the last statement is required. The other two show how it's done built into steps:
-- Set a session variable to the string
set INPUT_STRING = '["http://info.wealthenhancement.com/ppc-rt-retirement-planning"]';
-- Trim leading and trailing square brackets and double quotes
select (trim($INPUT_STRING, '"[]'));
-- Split using // as a delimiter and keep only the right part and cast as string
select split((trim($INPUT_STRING, '"[]')), '//')[1]::string as URL

Codename One - String replace with empty character

I like to normalize the phone numbers I get from the contacts in the local phone book. To do that, I want to remove any spaces, dashes, plus signs etc from the number.
CN1 only offers the String.replace(oldchar, newchar) function, instead of String operations. From this post,
How to represent empty char in Java Character class, this should be the way to go:
primaryPhoneNumber = primaryPhoneNumber.replace(' ', Character.MIN_VALUE);
however, this approach has several implications.
the char in the console output looks like a space, but its not. its a string terminator.
+49 234-63446
0 234 63446
when using this normalized string literal, including the Character.Min_Value in a database, the database query involving this string crashes:
Caused by: org.postgresql.util.PSQLException: ERROR: invalid byte sequence for encoding "UTF8": 0x00
How to properly remove spaces and other chars and replace them with a "nothing" character?
You can use:
String p = StringUtils.replaceAll(phone, " ", "");

Regex to reject if all numbers and reject colon

I am trying for a regex to
reject if input is all numbers
accept alpha-neumeric
reject colon ':'
I tried ,
ng-pattern="/[^0-9]/" and
ng-pattern="/[^0-9] [^:]*$/"
for example ,
"Block1 Grand-street USA" must be accepted
"111132322" must be rejected
"Block 1 grand : " must be rejected
You may use
ng-pattern="/^(?!\d+$)[^:]+$/"
See the regex demo.
To only forbid a : at the end of the string, use
ng-pattern="/^(?!\d+$)(?:.*[^:])?$/"
See another regex demo
The pattern matches
^ - start of string
(?!\d+$) - no 1+ digits to the end of the string
[^:]+ - one or more chars other than :
(?:.*[^:])? - an optional non-capturing group that matches 1 or 0 occurrences of
.* - any 0+ chars other than line break chars, as many as possible
[^:] - any char other than : (if you do not want to match an empty string, replace the (?: and )?)
$ - end of string.
According to comments, you want to match any character but colon.
This should do the job:
ng-pattern="/^(?!\d+$)[^:]+$/"

SQL Select statement until a character

I'm looking to extract all the text up until a '\' (backslash).
The substring is required to remove all proceeding characters (17 in total) and so I would like to return all after the 17th until it comes across a backslash.
I've tried using charindex but it doesn't seem to stop at the \ it returns characters afterward. My code is as follows
SELECT path, substring(path,17, CHARINDEX('\',Path)+ LEN(Path)) As Data
FROM [Table].[dbo].[Projects]
WHERE Path like '\ENQ%\' AND
Deleted = '0'
Example
The below screen shot shows the basic query and result i.e the whole string
I then use substring to remove the first X characters as there will always be the same amount of proceeding characters
But what Im actually after is (based on the above result) the "Testing 1" "Testing 2" and "Testing ABC" section
The substring is required to remove all proceeding characters (17 in total) and so I would like to return all after the 17th until it comes across a backslash.
select
substring(path,17,CHARINDEX('\',Path)-17)
from
table
To overcome Invalid length parameter passed to the LEFT or SUBSTRING function error, you can use CASE
select
substring(path,17,
CASE when CHARINDEX('\',Path,17)>0
Then CHARINDEX('\',Path)-17)
else VA end
)
from
table

How do I set word delimiters?

User's guide chapter 6.1.5 The Word Chunk A word is a string of characters delimited by space, tab, or return characters or enclosed by double quotes. Is it possible to have additional word delimiters?
I have the following code snippet taken from the User's Guide chapter 6.5.1 'When to use arrays', p. 184
on mouseUp
--cycle through each word adding each instance to an array
repeat for each word tWord in field "sample text"
add 1 to tWordCount[tWord]
end repeat
-- combine the array into text
combine tWordCount using return and comma
answer tWordCount
end mouseUp
It counts the number of occurences of each word form in the field "Sample text".
I realize that full stops after words are counted as part of the word with the default setting.
How do I change the settings that a full stop (and, or a comma) is considered a word boundary?
Alternatively you could simply remove the offending characters before processing.
This can be done using either the REPLACE function or the "REPLACETEXT function.
The REPLACETEXT function can use a regular expression matchstring but is slower than the REPLACE function. So here I am using the REPLACE function.
on mouseUp
put field "sample" into twords
--remove all trailing puncuation and quotes
replace "." with "" in twords
replace "," with "" in twords
replace "?" with "" in twords
replace ";" with "" in twords
replace ":" with "" in twords
replace quote with "" in twords
--hyphenated words need to be seperated?
replace "-" with " " in twords
repeat for each word tword in twords
add 1 to twordcount[tword]
end repeat
combine twordcount using return and comma
answer twordcount
end mouseUp
I think you are asking a question about delimiters. Some delimiters are built-in:
spaces for words,
commas for items,
return (CR) for lines.
The ability to create your own custom delimiter property (the itemDelimiter) is a powerful feature of the language, and pertains to "items". You can set this to any single character:
set the itemDelimiter to "C"
answer the number of items in "XXCXXCXX" --call this string "theText"
The result will be "3"
As others have pointed out, the method of replacing one string for another allows formidable control over custom parsing of text:
replace "C" with space in theText
yields "XX XX XX"
Craig Newman
As the User's guide says in chapter 6.1.5 The Word Chunk A word is a string of characters delimited by space, tab, or return characters or enclosed by double quotes.
There is itemDelimiter but not wordDelimiter.
So punctuation as to be removed first before adding the word to the word count array.
This may be done with a function effectiveWord.
function effectiveWord aWord
put last char of aWord into it
if it is "." then delete last char of aWord
if it is "," then delete last char of aWord
if it is ":" then delete last char of aWord
if it is ";" then delete last char of aWord
return aWord
end effectiveWord
on mouseUp
--cycle through each word adding each instance to an array
repeat for each word tWord in field "Sample text"
add 1 to tWordCount[effectiveWord(tWord)]
end repeat
-- combine the array into text
combine tWordCount using return and comma
answer tWordCount
end mouseUp

Resources