Posts

Showing posts with the label Regex

Allow Only Numbers And Dot In Script

Answer : This is a great place to use regular expressions. By using a regular expression, you can replace all that code with just one line. You can use the following regex to validate your requirements: [0-9]*\.?[0-9]* In other words: zero or more numeric characters, followed by zero or one period(s), followed by zero or more numeric characters. You can replace your code with this: function validate(s) { var rgx = /^[0-9]*\.?[0-9]*$/; return s.match(rgx); } That code can replace your entire function! Note that you have to escape the period with a backslash (otherwise it stands for 'any character'). For more reading on using regular expressions with javascript, check this out: http://www.regular-expressions.info/javascript.html You can also test the above regex here: http://www.regular-expressions.info/javascriptexample.html Explanation of the regex used above: The brackets mean " any character inside these brackets ." Y...

C# Regex For Guid

Answer : This one is quite simple and does not require a delegate as you say. resultString = Regex.Replace(subjectString, @"(?im)^[{(]?[0-9A-F]{8}[-]?(?:[0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?$", "'$0'"); This matches the following styles, which are all equivalent and acceptable formats for a GUID. ca761232ed4211cebacd00aa0057b223 CA761232-ED42-11CE-BACD-00AA0057B223 {CA761232-ED42-11CE-BACD-00AA0057B223} (CA761232-ED42-11CE-BACD-00AA0057B223) Update 1 @NonStatic makes the point in the comments that the above regex will match false positives which have a wrong closing delimiter. This can be avoided by regex conditionals which are broadly supported. Conditionals are supported by the JGsoft engine, Perl, PCRE, Python, and the .NET framework. Ruby supports them starting with version 2.0. Languages such as Delphi, PHP, and R that have regex features based on PCRE also support conditionals. (source http://www.regular-expressions.info/c...

Check For Camel Case In Python

Answer : You could check if a string has both upper and lowercase. def is_camel_case(s): return s != s.lower() and s != s.upper() and "_" not in s tests = [ "camel", "camelCase", "CamelCase", "CAMELCASE", "camelcase", "Camelcase", "Case", "camel_case", ] for test in tests: print(test, is_camel_case(test)) Output: camel False camelCase True CamelCase True CAMELCASE False camelcase False Camelcase True Case True camel_case False Convert your string to camel case using a library like inflection . If it doesn't change, it must've already been camel case. from inflection import camelize def is_camel_case(s): # return True for both 'CamelCase' and 'camelCase' return camelize(s) == s or camelize(s, False) == s

Can I Replace Groups In Java Regex?

Answer : Use $n (where n is a digit) to refer to captured subsequences in replaceFirst(...) . I'm assuming you wanted to replace the first group with the literal string "number" and the second group with the value of the first group. Pattern p = Pattern.compile("(\\d)(.*)(\\d)"); String input = "6 example input 4"; Matcher m = p.matcher(input); if (m.find()) { // replace first number with "number" and second number with the first String output = m.replaceFirst("number $3$1"); // number 46 } Consider (\D+) for the second group instead of (.*) . * is a greedy matcher, and will at first consume the last digit. The matcher will then have to backtrack when it realizes the final (\d) has nothing to match, before it can match to the final digit. You could use Matcher#start(group) and Matcher#end(group) to build a generic replacement method: public static String replaceGroup(String regex, String source, int groupT...

Click An Exact Match Text In Cypress

Answer : Regular Expressions will work nicely here. .contains() allows for regex So you can do a regex that matches the whole string only (use ^ and $ ). That way anything with extra characters won't match (like New Navigation Label). So for example, you could do: cy.get(`[data-test="dropdown"]`) .find('.item') .contains(/^Navigation Label$/) .click(); Regex is a little tricky when you are building an expression with a variable (ex. your option variable). In this case, you'll build a regular expression like so: cy.get(`[data-test="dropdown"]`) .find('.item') .contains(new RegExp("^" + option + "$", "g")) .click(); So to get an exact match with .contains() : cy.contains(new RegExp(yourString, "g")) You can use below code snippet to click on an element which has exact text. This will work like charm, let me know if you face any issue. You have to handle lik...

Bash Regular Expression -- Can't Seem To Match Any Of \s \S \d \D \w \W Etc

Answer : Perhaps \S and \s is not supported or that you can't place them around [ ] . Try to use this format instead: ^Disk[[:space:]]+/dev[^[:space:]]+:[[:space:]]+[^[:space:]]+ EDIT It seems you actually want to get the matching fields. I made the script simpler to this but I'm not sure if it's your intended output: #!/bin/bash regex='^Disk[[:space:]]+(/dev[^[:space:]]+):[[:space:]]+(.*)' while read line; do [[ $line =~ $regex ]] && echo "${BASH_REMATCH[1]} matches ${BASH_REMATCH[2]}." done < disks.txt Which produces /dev/sda matches 42.9GB. /dev/sdb matches 42.9GB. Because this is a common FAQ, let me list a few constructs which are not supported in Bash, and how to work around them, where there is a simple workaround. There are multiple dialects of regular expressions in common use. The one supported by Bash is a variant of Extended Regular Expressions. This is different from e.g. what many online regex testers supp...

C++ - Split String By Regex

Answer : #include <regex> std::regex rgx("\\s+"); std::sregex_token_iterator iter(string_to_split.begin(), string_to_split.end(), rgx, -1); std::sregex_token_iterator end; for ( ; iter != end; ++iter) std::cout << *iter << '\n'; The -1 is the key here: when the iterator is constructed the iterator points at the text that precedes the match and after each increment the iterator points at the text that followed the previous match. If you don't have C++11, the same thing should work with TR1 or (possibly with slight modification) with Boost. To expand on the answer by @Pete Becker I provide an example of resplit function that can be used to split text using regexp: #include <regex> std::vector<std::string> resplit(const std::string & s, std::string rgx_str = "\\s+") { std::vector<std::string> elems; std::regex rgx (rgx_str); std::sregex_token_iterator iter(s.begin(...

^a-zA-Z0-9 Excluding Spaces?

Answer : You could also try with: /[^a-zA-Z0-9\s]/gi If you only want to exclude spaces use: [^ ]* Test the regex here if you want.