I need a substring from JsonPath - jsonpath

The given string is:
{"POWER":"ON","Dimmer":2,"Color":"12345678","HSBColor":"120,100,2","Channel":[0,1,0,0]}
result should be:
123456
In the Android app MQTT Dash I need only the first six characters from the color.
With $.Color I get:
Invalid color format

I did the following for a sonoff/trasmota color bulb:
1) Leave blank the field "Extract from JSON Path"
2) on the event "On Receive", write the following code:
event.payload = JSON.parse(event.payload)['Color'].substring(0,6);
Hope this helps.
Cheers.

Related

How to extract a substring from main string starting from valid uuid using lua

I have a main string as below
"/tmp/xjtscpdownload/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/"
From the main string i need to extract a substring starting from the uuid part
"/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/"
I tried
string.match("/tmp/xjtscpdownload/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/", "/[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}/(.)/(.)/$"
But noluck.
if you want to obtain
"/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/"
from
"/tmp/xjtscpdownload/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/"
or let's say 7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0, output and 9999.317528060546245771146821638997525068657 as this is what your pattern attempt suggests. Otherwise leave out the parenthesis in the following solution.
You can use a pattern like this:
local text = "/tmp/xjtscpdownload/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/"
print(text:match("/([%x%-]+)/([^/]+)/([^/]+)"))
"/([^/]+)/" captures at least one non-slash-character between two slashs.
On your attempt:
You cannot give counts like {4} in a string pattern.
You have to escape - with % as it is a magic character.
(.) would only capture a single character.
Please read the Lua manual to find out what you did wrong and how to use string patterns properly.
Try also the code
s="/tmp/xjtscpdownload/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/"
print(s:match("/.-/.-(/.+)$"))
It skips the first two "fields" by using a non-greedy match.

GA Search & Replace Filter

I wonder whether someone can help me please.
I have the following URI in GA: /invite/accept-invitation/accepted/B
Which I'd like to change to: /invite/accept-invitation/accepted
I've tried a 'Search and Replace filter as follows:
Search String - /invite/accept-invitation/accepted/*
Replace String - /invite/accept-invitation/accepted
But the result I get is:
/inviteaccept-invitation/accepted/B
Could someone tell me where I've gone wrong with this please?
Many thanks and kind regards
Chris
Google Analytics "Search and replace" filter uses regular expressions. More precisely:
Replace string is either a regular string or it can refer to group
patterns in the search expression using backslash-escaped single
digits like (\0 to \9).
More details are available on the filter settings UI, which also refers to this link.
So in your case, the search string would be something like this.
\/invite\/accept-invitation\/accepted\/\w+
In this expression \ is escaped. Your last string part is captured with \w+, which
matches any word character (equal to [a-zA-Z0-9_]), between one and unlimited times, as many times as possible.
The Replace string doesn't have to be a regular expression. So in your case, your original version could be used:
/invite/accept-invitation/accepted/
Putting this together would result something like this, which gives the desired output in my test view:

Problems pattern matching using R regular expressions

I'm trying to extract a string using str_extract. Here is a small example of the type of string:
library(stringr)
gs<-"{\"type\":\"Polygon\",\"coordinates\":[[[1,2],[3,4],[5,6],[7,8]]]}"
s='\\{\\\"type\\\"*\\}'
str_extract(gs,s)
I'd like to get a print-out of the entire string (the real string will have more characters of this type and should only return the piece I specified here). Instead I get NA. I'd be grateful for any ideas as to what I'm doing wrong. Thank you!
Does this do what you want?
gs<-"{\"type\":\"Polygon\",\"coordinates\":[[[1,2],[3,4],[5,6],[7,8]]]} I DO NOT WANT THIS {\"type\":\"Not a Polygon\",\"coordinates\":[[[1,2],[3,4],[5,6],[7,8]]]}"
s="\\{\"type\"(.*?)\\}"
result = str_match_all(gs,s)[[1]][,1]
To test, I added the string 'I DO NO WANT THIS', which should not be returned, and added a second object which type is 'not a polygon'
It returns:
"{\"type\":\"Polygon\",\"coordinates\":[[[1,2],[3,4],[5,6],[7,8]]]}"
"{\"type\":\"Not a
Polygon\",\"coordinates\":[[[1,2],[3,4],[5,6],[7,8]]]}"
So only the elements requested. Hope this helps!

RegularExpression Validator For Textbox

In my requirement a Textbox should allow Alphabets,Numeric s, Special Characters,Special Symbols With at least one Alphabet.
I will try like this but i am not getting.
^\d*[a-zA-Z][a-zA-Z0-9#*,$._&% -!><^#]*$
You may want to have 2 regular expression validators; one for validating the allowed characters, and one for validating that at least on alphabet has been provided. You may be able to get at least one, but this way, you can have two separate validation messages to show the user explaining why the input is wrong.
Just match for special characters until you encounter a letter, then match for everything until the end of the string:
^[0-9#*,$._&% -!><^#]*[a-zA-Z0-9#*,$._&% -!><^#]*$
Use lookaheads :
/^(?=.*[a-zA-Z])[\w#*,$.&%!><^#-]*$/
Edit :
I assume the - is meant as the actual - character and not a range of space to !.
I removed the space character. You can of course add it if you want.
[ -!]
Effectively means :
[ -!] # Match a single character in the range between “ ” and “!”
And I have no idea what that range entails!

Text box validation using regular expression

I have a textbox and have to use the regular expressions in asp.net
My text should not allow the spaces in first and last place. In between words it can allow.
that means: it should allow alphabets, numbers and all special characters.
Output should be:
India Bangalore -valid
India Bangalore - not valid
!India bangalore - valid
India bangalore!##$%- valid
IndiaBangalore - valid
i.e : use can enter the spaces in between the words but not in first position and last position.
Java script also fine.
Thanks in advance.
Here is a quick example
// Input string
string st = " This is an example string. ";
// Call Trim instance method.
// This returns a new string copy.
st = st.Trim();
U can get the index, i mean index(0) is the first position of textbox and calculate the length of ur input +1 = last index of ur input validate white space validation now. damn sure it will work

Resources