Whats wrong with my isNumber regex? - regex

why this function will return "e10" as true? (which is supposed to be false)
public boolean isNumber(String s) {
String pattern = "\\s*[+-]?((\\d+.?\\d*)|.\\d+)(e[+-]?\\d+)?\\s*";
return s.matches(pattern);
}

Because of ((\\d+.?\\d*)|.\\d+). The second part means . - a matcher for anything, and \d+ - at least one digit.
If you meant to match an actual dot character, use \. instead.

Related

regex to extract substring for special cases

I have a scenario where i want to extract some substring based on following condition.
search for any pattern myvalue=123& , extract myvalue=123
If the "myvalue" present at end of the line without "&", extract myvalue=123
for ex:
The string is abcdmyvalue=123&xyz => the it should return myvalue=123
The string is abcdmyvalue=123 => the it should return myvalue=123
for first scenario it is working for me with following regex - myvalue=(.?(?=[&,""]))
I am looking for how to modify this regex to include my second scenario as well. I am using https://regex101.com/ to test this.
Thanks in Advace!
Some notes about the pattern that you tried
if you want to only match, you can omit the capture group
e* matches 0+ times an e char
the part .*?(?=[&,""]) matches as least chars until it can assert eiter & , or " to the right, so the positive lookahead expects a single char to the right to be present
You could shorten the pattern to a match only, using a negated character class that matches 0+ times any character except a whitespace char or &
myvalue=[^&\s]*
Regex demo
function regex(data) {
var test = data.match(/=(.*)&/);
if (test === null) {
return data.split('=')[1]
} else {
return test[1]
}
}
console.log(regex('abcdmyvalue=123&3e')); //123
console.log(regex('abcdmyvalue=123')); //123
here is your working code if there is no & at end of string it will have null and will go else block there we can simply split the string and get the value, If & is present at the end of string then regex will simply extract the value between = and &
if you want to use existing regex then you can do it like that
var test = data1.match(/=(.*)&|=(.*)/)
const result = test[1] ? test[1] : test[2];
console.log(result);

java regex pattern.compile Vs matcher

Im trying to find whether a word contains consecutive identical strings or not, using java.regex.patterns, while testing an regex with matcher, It returns true. But if I only use like this :
System.out.println("test:" + scanner.hasNext(Pattern.compile("(a-z)\\1")));
it returns false.
public static void test2() {
String[] strings = { "Dauresselam", "slab", "fuss", "boolean", "clap", "tellme" };
String regex = "([a-z])\\1";
Pattern pattern = Pattern.compile(regex);
for (String string : strings) {
Matcher matcher = pattern.matcher(string);
if (matcher.find()) {
System.out.println(string);
}
}
}
this returns true. which one is correct.
The pattern ([a-z])\\1 uses a capturing group to match a single lowercase character which is then followed by a backreference to what is captured in group 1.
Ih you have Dauresselam for example, it would match the first s in the capturing group and then matches the second s. So if you want to match consecutive characters you could use that pattern.
The pattern (a-z)\\1 uses a capturing group to match a-z literally and then then uses a backreference to what is captured in group 1. So that would match a-za-z
It depends on what you want. Here you use parenthesis:
Pattern.compile("(a-z)\\1").
Here you use Square brackets inside pareanthesis:
String regex = "([a-z])\\1";
To compare, you should obviously use the same pattern.

Regular Expressions that is true if the word "bad" is not in the string

I am parsing a feed and need to exclude fields that consist of a string with the word "bad", in any combination of case.
For example "bad" or "Bad id" or "user has bAd id" would not pass the regular expression test,
but "xxx Badlands ddd" or "aaabad" would pass.
Exclude anything that matches /\bbad\b/i
The \b matches word boundaries and the i modifier makes it case insensitive.
For javascript, you can just put your word in the regex and do the match \b stnads for boundries, which means no character connected :
/\bbad\b/i.test("Badkamer") // i for case-insensitive
You may try this regex:
^(.*?(\bbad\b)[^$]*)$
REGEX DEMO
I think the easiest way to do this would be to split the string into words, then check each word for a match, It could be done with a function like this:
private bool containsWord(string searchstring, string matchstring)
{
bool hasWord = false;
string[] words = searchstring.split(new Char[] { ' ' });
foreach (string word in words)
{
if (word.ToLower() == matchstring.ToLower())
hasWord = true;
}
return hasWord;
}
The code converts everything to lowercase to ignore any case mismatches. I think you can also use RegEx for this:
static bool ExactMatch(string input, string match)
{
return Regex.IsMatch(input.ToLower(), string.Format(#"\b{0}\b", Regex.Escape(match.ToLower())));
}
\b is a word boundary character, as I understand it.
These examples are in C#. You didn't specify the language

regular expressions, delimiting plus sign

Private Const SEPARATOR_REG_EXP1 As String = "SCD\+4\+[A-Z]\+"
Public Function TestReg() As Boolean
Dim s1 As String = "SCD+4+ADJUSTMENT+"
Dim match As Match = Regex.Match(s1, SEPARATOR_REG_EXP1)
If match.Success Then
Return True
Else : Return False
End If
End Function
Not sure why this does not match - haven't really used regular expressions much.
The regex pattern should be :
"SCD\+4\+[A-Z]+\+"
You have to add a + sign after [A-Z], because you want to match one or multiple of these [A-Z] characters.
This does not match, because [A-Z]matches only a single character of the given character class. You can use the + quantifier to match multiple chars. The resulting RegEx would be
SCD\+4\+[A-Z]+\+

Regular expression match decimal with letters

I have following string 3.14, 123.56f, .123e5f, 123D, 1234, 343E12, 32.
What I want to do is match any combination of above inputs. So far I started with the following:
^[0-9]\d*(\.\d+)
I realize I have to escape the . since its a regular expression itself.
Thanks.
This should also work, if not already proposed.
try {
Pattern regex = Pattern.compile("\\.?\\b[0-9]*\\.?[0-9]+(?:[eE][-+]?[0-9]+)?[fD]?\\b", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) {
// matched text: regexMatcher.group()
// match start: regexMatcher.start()
// match end: regexMatcher.end()
}
} catch (PatternSyntaxException ex) {
// Syntax error in the regular expression
}
Probably
^(\d+(\.\d+)?|\.\d+)([eE]\d+)?[fD]?$
http://regexr.com?2ut9t
^ start of the string
(\d+(\.\d+)?|\.\d+) one or more digits with an optional ( . and one or more digits)
or
. and one or more digits
([eE]\d+)? an optional ( e or E and one or more digits)
[fD]? an optional f or D
$ end of the string
As a sidenote, I've made the D compatible with everything but the f.
If you need positive and negative sign, add [+-]? after the ^
This will match all of those:
[0-9.]+(?:[Ee][0-9.]*)?[DdFf]?
Note that within a character class (square brackets), dot . is not a special character and should not be escaped.
Maybe that one ?
^\d*(?:\.\d+)?(?:[eE]\d+)?(?:[fD])?$
with
^\d* #possibly a digit or sequence of digits at the start
(?:\.\d+)? #possibly followed by a dot and at least one digit
(?:[eE]\d+)? #possibly a 'e' or 'E' followed by at least one digit
(?:[fD])?$ #optionnaly followed by 'f' or 'D' letters until the end
You can use regexpal to test it out, but this seems to work on all of those examples:
^\d*\.?(\d*[eE]?\d*)[fD]?$