Java 8 Matcher says there are matches but groupCount is 0 [duplicate] - regex

This question already has answers here:
Java RegEx Matcher.groupCount returns 0
(4 answers)
Closed 5 years ago.
Java 8 here. I have the following function:
public String extractArgs(String function) {
Pattern inRegex = Pattern.compile("in\\(.*\\)");
Matcher inMatch = inRegex.matcher(function);
log("num in(...) function matches: " + inMatch.groupCount() + "but does inMatch.matches()? " + inMatch.matches());
if(inMatch.groupCount() > 0) {
return inMatch.group(1);
} else {
return "";
}
}
When I pass it "in(hello)" as an argument, I get the following output:
num in(...) function matches: 0 but does inMatch.matches()? true
My understanding is that if inMatch.matches() returns true (which is happening), that I should have at least one match group (inMatch.groupCount > 0).
I'm trying to compare the inputted arg string against the regex and (if there is a match) obtain the blurb of text that is contained inside the "in(...) function". Hence if I call extractArgs("in(hello)") then it should return the string "hello". Where am I going awry?!

There is no capture groups in your original regex. To introduce a capture group (which you want), you should define the regular expression along the lines of:
in\\((.*)\\)
Note that I've added a () brackets around where the arguments supposed to go. This way I have created a capture group around .*, so you will now have the groups:
pattern.matcher("in(hello, World!)").group(1) // hello, World!

Related

.NET Core - regex matches whole string instead of group [duplicate]

This question already has answers here:
Returning only part of match from Regular Expression
(4 answers)
Closed 2 years ago.
I tested my regex on regex101.com, it returns 3 groups
text :
<CloseResponse>SESSION_ID</CloseResponse>
regex :
(<.*>)([\s\S]*?)(<\/.*>)
in C#, I get only one match and one group that contains the whole string instead of just the SESSION_ID
I expect the code to return only SESSION_ID
I tried finding a global option but there don't seem to be any
here is my code
Regex rg = new Regex(#"<.*>([\s\S]*?)<\/.*>");
MatchCollection matches = rg.Matches(tag);
if (matches.Count > 0) ////////////////////////////////// only one match
{
if (matches[0].Groups.Count > 0)
{
Group g = matches[0].Groups[0];
return g.Value; //////////////////// = <CloseResponse>SESSION_ID</CloseResponse>
}
}
return null;
thanks for helping me on this
I managed to make it work this way
string input = "<OpenResult>SESSION_ID</OpenResult>";
// ... Use named group in regular expression.
Regex expression = new Regex(#"(<.*>)(?<middle>[\s\S]*)(<\/.*>)");
// ... See if we matched.
Match match = expression.Match(input);
if (match.Success)
{
// ... Get group by name.
string result = match.Groups["middle"].Value;
Console.WriteLine("Middle: {0}", result);
}
// Done.
Console.ReadLine();
Use non-capturing group if you want whole string as result: (?:)
(?:<.*>)(?:[\s\S]*?)(?:<\/.*>)
Demo
If you just want to capture session id use this:
(?:<.*>)([\s\S]*?)(?:<\/.*>)
Demo

Get the number between two characters - Typescript [duplicate]

This question already has answers here:
RegExp in TypeScript
(5 answers)
Closed 2 years ago.
I am new to Typescript and trying to make a webhook in my Google Cloud Functions.
I have a string: C1234567890A460450P10TS1596575969702
I want to use regex to extract the number 1234567890 from that string.
The first character C is fixed and does not change, the character A after the number is variable and can be any other alphabet.
The regex that matches the number is (?<=C)(\d{10})(?=\w).
I want to know how to execute this regex in Typescript so that I can get the number into a variable(eg: const number = [the number extracted from the string] //value 1234567890)
Edit 1:
Based on the provided suggestions (which I had tried already before posting this question), here is the code I could make out of it:
const string = request.body.string;
let regxp = new RegExp('(?<=C)(\d{10})(?=\w)');
const number = regxp.exec(string);
response.send(number);
This gives a blank response.
There is two problems, you never parsed the returned string to a number with parseInt and (?<=C) (positive lookbehind) is not always supported.
Second, your regular expression can be simplified into ^C\d{10} and a .splice(1) to remove the C.
const string: string = request.body.string;
const matches = s.match(/^C\d{10}/);
let number: number;
if(matches !== null) {
number = parseInt(matches[0].slice(1));
} else {
res.status(400).end(); // Assuming this is express
return;
}
res.send(number); // 1234567890
Playground

Issue with REGEX in javascript [duplicate]

This question already has answers here:
How to tell if a string contains a certain character in JavaScript?
(21 answers)
Closed 4 years ago.
I get an issue using REGEX, it is probably about my REGEX but I need some helps.
I need to match all string containing "D"...
Test string 1 : D
Test string 2 : aaaaaaDqqqqq
Test string 3 : Dssssssss
Test string 4 : D4564646
Test string 5 : 1321313D2312
Test string 6 : ppppprrrrrr
My regex :
/^.+D.+|(:?^|\s)D$/gi
It works only for 1 and 2 and it should works for 1, 2, 3, 4 and 5.
In your case problem is with + operator which is literally Matches between one and unlimited times so it wont work if letter "D" will be in the beggining or the end of string. Try this regex: ^.*D.*$ with asterix, as it is defined as Matches between zero and unlimited times
See example
Following regex should work for you
.*D.*
If all you need to do is test for whether or not a string contains a D character, it's just /D/
var tests = [
"D",
"aaaaaaDqqqqq",
"Dssssssss",
"D4564646",
"1321313D2312",
"ppppprrrrrr"
]
tests.forEach(str => console.log(/D/.test(str)))
Instead of using a regex simply use the includes function
var string = "aaaaaaDqqqqq",
substring = "D";
if(string.includes(substring)){
console.log("contain")
}else{
console.log("don't contain")
}

How to use \R in java 8 regex [duplicate]

This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 5 years ago.
I am trying to use the new \R regex matcher from java 8.
However, for the following code :
public static void main(String[] args)
{
String s = "This is \r\n a String with \n Different Newlines \r and other things.";
System.out.println(s);
System.out.println(Pattern.matches("\\R", s));
if (Pattern.matches("\\R", s)) // <-- is always false
{
System.out.println("Matched");
}
System.out.println(s.replaceAll("\\R", "<br/>")); //This is a String with <br/> Different Newlines <br/> and other things.
}
The Pattern.matches always returns false, where as the replaceAll method does seem to find a match and does what I want it to. How do I make the Pattern.matches work ?
I have also tried the long about way and still can't get it to work :
Pattern p = Pattern.compile("\\R");
Matcher m = p.matcher(s);
boolean b = m.matches();
System.out.println(b);
Well matches (both in String and Matchers classes) attempts to match the complete input string.
You need to use matcher.find instead:
Pattern p = Pattern.compile("\\R");
Matcher m = p.matcher(s);
boolean b = m.find();
System.out.println(b);
From Java docs:
\R Matches any Unicode line-break sequence, that is equivalent to \u000D\u000A|[\u000A\u000B\u000C\u000D\u0085\u2028\u2029]
PS; If you want to know if input contains a line-break then this one liner will work for you:
boolean b = s.matches("(?s).*?\\R.*");
Note use of .* on either side of \R to make sure we are matching complete input. Also you need (?s) to enable DOTALL mode to be able to match multiline string with .*

Regular expression exact part of letters match in middle of the word including Dot[.] [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
I am looking for regular expression for exact letters in middle of word. which should match Dot(.) also.
Currently I am using regular expression is "\\w*."+inputString +"\\w*", "i" , actually period represents any letter in this expression.
eg:
inputData = {name:[abc.12, abcdef, bc1454, test, rahul, bc.reju, rewbc.]}
inputString = "bc."
var wordFormat = new RegExp('\\w*'+inputString +'\\w*', 'i');
workFormat.test(inputData);
scenario 1: Starting of word.
input : 'bc.'
actual output is: abc.12
expecting output is: abc.12, bc.reju, rewbc.
expect output should get only one because passing inputString matches only one item in array of object (inputData) so, expecting output item is 1.
Here is a demo - Regex101
You can modify this by replacing the "bc" with your search string.
\w*bc\w*\.\d*
updated Expression
\w*bc\.\d*
Here is the example that you can use as your requirement:
regular = "bc." //These was the actual input expression you want to test
var regularplacholder = '[character]';
var regularexpresion="/";
for(var index =0;index<regular.length;index++)
{
regularexpresion+=(regularplacholder.replace('character',regular[index]));
}
regularexpresion+='*'
if(regularexpresion.test('test string')){
//your logic here
}