I want to create a Regular Expression which will only the input string to contain only [G|y|M|d|D|F|E|h|H|m|s|S|w|W|a|z|Z] so I come up with some code from below:
std::regex Reg = regex("[G|y|M|d|D|F|E|h|H|m|s|S|w|W|a|z|Z]");
My problem is that my regex still not correct because my input string can contain other characters with the characters in the above group such as:
std::string myInputString = Gx //correct
Which Gx has to be wrong
I’m not a user of C++’s regex lib, but I understand it supports ECMAScript syntax. So I don’t think you need the pipe characters. The “any character in set” [] syntax doesn’t use pipes. Secondly, if you want to match the entire input string (instead of any part of it) you need to use the ^ and $ anchors
Try:
std::regex( "^[GyMdDFEhHmsSwWazZ]+$" );
What i can understand from your question is that, you want to input string with those chosen characters only
the regex is correct up to my knowledge
but you need to compare the string char by char because if you are not doing that you might get the similar results like you are getting now...
Related
I want to take a number input as a string and then strip redundant zeroes. I've done this using substring but was wondering if there's a way to do it using regex. For example, regex replace.
I only use a regex to check if the string is a valid number or not. I use substring and if conditions repeatedly for the strippings. I want to be able to convert say: "0012340.3200E6" to "12340.32E6" using regex.
rr("((\\+|-)?[[:digit:]]+)(\\.(([[:digit:]]+)?))?((e|E)((\\+|-)?)[[:digit:]]+)?");
You would be much better off converting the number into a float and then use string formatting, i.e. using
char buffer [50];
sprintf(buffer, "%E", ...)
)
or spritnf(buffer, "%fE%d", ..., ...) if for whatever you're doing this should work better.
Anyways, the regex should look something like
([+-]?)0*(\d+)(?:(\.\d*[1-9])|\.)0*(?:([Ee][+-]?)0*(\d+))?
(see regex101.com) and the substitution pattern would then be
$1$2$3$4$5
so you have everything in matching groups except the zeros and then you replace it with the content of all matching groups.
What's left is to escape the above expression and deal with the \d for digits – I've seen you're using [[:digit:]] instead.
Am trying to form Regex for the below pattern for string
String may contain alphanumeric characters
String may contain the following special characters like Space ( ) - :/
If the string contains anything apart from this my Regex should return false
I have tried with below regex [0-9a-zA-Z\s():-]+ but it is not working out. match is returning true even if it contais characters like ,; etc
am able to achieve for blacklisting but am trying to achieve what characters are allowed if anything othet than that found return false
Some one who is good in writing regular expressions can help me out.
Thanks
Make sure to use start/end anchors to avoid matching unwanted input data:
^[0-9a-zA-Z \/():-]+$
I have trouble working on this regular expression.
Here is the string in one line, and I want to be able to extract the thing in the swatchColorList, specifically I want the word Natural Burlap, Navy, Red
What I have tried is '[(.*?)]' to get everything inside bracket, but what I really want is to do it in one line? is it possible, or do I need to do this in two steps?
Thanks
{"id":"1349306","categoryName":"Kids","imageSource":"7/optimized/8769127_fpx.tif","swatchColorList":[{"Natural Burlap":"8/optimized/8769128_fpx.tif"},{"Navy":"5/optimized/8748315_fpx.tif"},{"Red":"8/optimized/8748318_fpx.tif"}],"suppressColorSwatches":false,"primaryColor":"Natural Burlap","clickableSwatch":true,"selectedColorNameID":"Natural Burlap","moreColors":false,"suppressProductAttribute":false,"colorFamily":{"Natural Burlap":"Ivory/Cream"},"maxQuantity":6}
You can try this regex
(?<=[[,]\{\")[^"]+
If negative lookbehind is not supported, you can use
[[,]\{"([^"]+)
This will save needed word in group 1.
import json
str = '{"id":"1349306","categoryName":"Kids","imageSource":"7/optimized/8769127_fpx.tif","swatchColorList":[{"Natural Burlap":"8/optimized/8769128_fpx.tif"},{"Navy":"5/optimized/8748315_fpx.tif"},{"Red":"8/optimized/8748318_fpx.tif"}],"suppressColorSwatches":false,"primaryColor":"Natural Burlap","clickableSwatch":true,"selectedColorNameID":"Natural Burlap","moreColors":false,"suppressProductAttribute":false,"colorFamily":{"Natural Burlap":"Ivory/Cream"},"maxQuantity":6}'
obj = json.loads(str)
words = []
for thing in obj["swatchColorList"]:
for word in thing:
words.append(word)
print word
Output will be
Natural Burlap
Navy
Red
And words will be stored to words list. I realize this is not a regex but I want to discourage the use of regex on serialized object notations as regular expressions are not intended for the purpose of parsing strings with nested expressions.
I'm trying to replace slashes in a string, but not all of them - only the ones before first comma. To do that, I probably have to find a way to match only slashes being followed by string containing a comma.
Is it possible to do this using one regexp, i.e. without first splitting the string by commas?
Example input string:
Abc1/Def2/Ghi3,/Dore1/Mifa2/Solla3,Sido4
What I want to get:
Abc1.Def2.Ghi3,/Dore1/Mifa2/Solla3,Sido4
I've tried some lookahead and lookbehind techniques with no effect, so currently to do this in e.g. Python I first split the data:
test = 'Abc1/Def2/Ghi3,/Dore1/Mifa2/Solla3,Sido4'
strlist = re.split(r',', test)
result = ','.join([re.sub(r'\/', r'.', strlist[0])] + strlist[1:])
What I would prefer is to use a specific regexp pattern instead of Python-oriented solution though, so essentially I could have a pattern and replacement such that the following code would give me the same result:
result = re.sub(pattern, replacement, test)
Thanks for all regex-avoiding answers - I was wondering if I could do this using only regex (so e.g. I could use sed instead of Python).
item = 'Abc1/Def2/Ghi3,/Dore1/Mifa2/Solla3,Sido4'
print item.replace("/", ".", item.count("/", 0, item.index(",")))
This will print what you need. Try to avoid regex wherever you can because they are slow.
You could do this with lookbehind expressions that look for both the beginning of the string and no comma. Or don't use re entirely.
s = 'Abc1/Def2/Ghi3,/Dore1/Mifa2/Solla3,Sido4'
left,sep,right = s.partition(',')
sep.join((left.replace('/','.'),right))
Out[24]: 'Abc1.Def2.Ghi3,/Dore1/Mifa2/Solla3,Sido4'
I have a regular expression, defined in a verbatim C# string type, like so:
private static string importFileRegex = #"^{0}(\d{4})[W|S]\.(csv|cur)";
The first 3 letters, right after the regex line start (^) can be one of many possible combinations of alphabetic characters.
I want to do an elegant String.Format using the above, placing my 3-letter combination of choice at the start and using this in my matching algorithm, like this:
string regex = String.Format(importFileRegex, "ABC");
Which will give me a regex of ^ABC(\d{4})[W|S]\.(csv|cur)
Problem is, when I do the String.Format, because I have other curly braces in the string (e.g. \d{4}) String.Format looks for something to put in here, can't find it and gives me an error:
System.FormatException : Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
Anyone know how, short of splitting the strings out, I can escape the other curly braces or something to avoid the above error?
Try this (notice the double curly braces):
#"^{0}(\d{{4}})[W|S]\.(csv|cur)"