I am using the code inXML scehma to represent my phone number which is like this:
ABC-JHG
I am getting an error like: values Abc-JHG is not allowed for element Name.
How should I make it comaptile with my input.
\d is only for numbers, change the second two to \w.
Use a regular expression that allows non-numeric data.
Related
I am trying to formnulate a regex to get the ids from the below two strings examples:
/drugs/2/drug-19904-5106/magnesium-oxide-tablet/details
/drugs/2/drug-19906/magnesium-moxide-tablet/details
In the first case, I should get 19904-5106 and in the second case 19906.
So far I tried several, the closes I could get is [drugs/2/drug]-.*\d but would return g-19904-5106 and g-19907.
Please any help to get ride of the "g-"?
Thank you in advance.
When writing a regex expression, consider the patterns you see so that you can align it correctly. For example, if you know that your desired IDs always appear in something resembling ABCD-1234-5678 where 1234-5678 is the ID you want, then you can use that. If you also know that your IDs are always digits, then you can refine the search even more
For your example, using a regex string like
.+?-(\d+(?:-\d+)*)
should do the trick. In a python script that would look something like the following:
match = re.search(r'.+?-(\d+(?:-\d+)*)', my_string)
if match:
my_id = match.group(1)
The pattern may vary depending on the depth and complexity of your examples, but that works for both of the ones you provided
This is the closest I could find: \d+|.\d+-.\d+
I have an example text string text_var = 'ndTail7-40512-1' and I want to split the first time I see a number followed by a - BUT I want to keep the number. Currently, I have print(re.split('\d*(?=-)',text_var,1)) and my output is ['ndTail', '-40512-1']. But I want to keep that number which is the trigger so it should look like ['ndTail', '7-40512-1']. Any help?
We can try using re.findall here:
text_var = 'ndTail7-40512-1'
matches = re.findall(r'(.*?)(\d-.*$)', text_var)
print(matches[0])
This prints:
('ndTail', '7-40512-1')
Sometimes it can be easier to use re.findall rather than re.split.
The regex pattern used here says to:
(.*?) match AND capture all content up to, but including
(\d-.*$) the first digit which is followed by a hyphen;
match and capture this content all the way to the end of the input
Note that we are using re.findall which typically has the potential to return multiple matches. However, in this case, our pattern matches to the end of the input, so we are left with just a single tuple containing the two desired capture groups.
I have a parameter called Analyst group in this format :
[Dimension].[Analyst Group].&[Nl.Workplace.Foundation]
I want to pass this parameter to another report, to filter data. Its a multi value parameter. But the other report only accepts it in this format : [KanBan].[Analyst Group].&[Nl.Workplace.Foundation]
So im trying to isolate the "Nl.Workplace.Foundation", so i can do the following thing in the Go To Report parameter expression :="[KanBan].[Analyst Group].&["& --Isolated analyst group-- &"]" to create the desired format.
So what i need is to extract the part between .&[ and ]
But i really have no idea how to isolate that part of the string.
Found a solution! If i just use the Parameter.label instead of Parameter.value it automatically does what i want!
A different solution has been found, but I will still answer the initial question. It could help.
So what i need is to extract the part between .&[ and ]
You could use a regex.
This may not be the fastest way but it can handle most of the situations.
So let's assume you have a string containing:
[Dimension].[Analyst Group].&[Nl.Workplace.Foundation]
And you want to get the following string:
Nl.Workplace.Foundation
Just use the following expression:
=System.Text.RegularExpressions.Regex.Match("[Dimension].[Analyst Group].&[Nl.Workplace.Foundation]", "\.&\[(?<NWF>[^]]+)\]").Groups("NWF").Value
In the expression, replace the input string with your dynamic values, like for example:
=System.Text.RegularExpressions.Regex.Match(Fields!Dimension.Value & "." & Fields!AnalystGroup.Value, "\.&\[(?<NWF>[^]]+)\]").Groups("NWF").Value
I'm keeping the formula as simple as possible so that you can easily adapt it, with, say, handling the case where an input string will not have a match (with the above query it will return #Error).
You could do this by adding an IIF() or better, use a custom function that you can reuse in several places and will reduce the length of your expression.
given a string, I need to test if the string is in the following format [000,000,000,000] where 0 represents any number. However each of the 4 numbers could be 1,2 or 3 digits. If an error is detected, I need to throw a NumberFormatException and convert it to a FormatException. Thanks.
The easiest way would be to check if it matches a regular expression. Something like this would do the trick:
\[[0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]{1,3}\]
or this if you want a condensed version:
\[([0-9]{1,3},){3}[0-9]{1,3}\]
You didn't specify a language (I'd guess it's Java?), but they all have some regex functionality. Throwing the exception should be pretty simple for you, so I won't go into that...
I'm getting a garbled JSON string from a HTTP request, so I'm looking for a temp solution to select the JSON string only.
The request.params() returns this:
[{"insured_initials":"Tt","insured_surname":"Test"}=, _=1329793147757,
callback=jQuery1707229194729661704_1329793018352
I would like everything from the start of the '{' to the end of the '}'.
I found lots of examples of doing similar things with other languages, but the purpose of this is not to only solve the problem, but also to learn Scala. Will someone please show me how to select that {....} part?
Regexps should do the trick:
"\\{.*\\}".r.findFirstIn("your json string here")
As Jens said, a regular expression usually suffices for this. However, the syntax is a bit different:
"""\{.*\}""".r
creates an object of scala.util.matching.Regex, which provides the typical query methods you may want to do on a regular expression.
In your case, you are simply interested in the first occurrence in a sequence, which is done via findFirstIn:
scala> """\{.*\}""".r.findFirstIn("""[{"insured_initials":"Tt","insured_surname":"Test"}=, _=1329793147757,callback=jQuery1707229194729661704_1329793018352""")
res1: Option[String] = Some({"insured_initials":"Tt","insured_surname":"Test"})
Note that it returns on Option type, which you can easily use in a match to find out if the regexp was found successfully or not.
Edit: A final point to watch out for is that the regular expressions normally do not match over linebreaks, so if your JSON is not fully contained in the first line, you may want to think about eliminating the linebreaks first.