Link to home
Start Free TrialLog in
Avatar of jazzIIIlove
jazzIIIloveFlag for Sweden

asked on

asp.net (?:^|,)(?=[^"]|(")?)"?((?(1)[^"]*|[^,"]*))"?(?=,|$) to java regex

Hi;

I need to convert this regex to java

(?:^|,)(?=[^"]|(")?)"?((?(1)[^"]*|[^,"]*))"?(?=,|$)

Any helps?

Br.
Avatar of jitendra patil
jitendra patil
Flag of India image

In Java it can be done using the below sample code
import java.util.regex.Matcher;
import java.util.regex.Pattern;

 // String to be scanned to find the pattern.
 String line = "your line where you want to match the regex";
 String pattern = "(?:^|,)(?=[^"]|(")?)"?((?(1)[^"]*|[^,"]*))"?(?=,|$)";

// Create a Pattern object
  Pattern r = Pattern.compile(pattern);

 // Now create matcher object.
    Matcher m = r.matcher(line);
    if (m.find( )) {
         System.out.println("Found value: " + m.group(0) );
         System.out.println("Found value: " + m.group(1) );
         System.out.println("Found value: " + m.group(2) );
      } else {
         System.out.println("NO MATCH");
      }

Open in new window

for more reference please visit the link below
Java - Regular Expressions
In ASp.Net it can be done with the below sample code
Add a using statement to reference the System.Text.RegularExpressions namespace.
using System.Text.RegularExpressions;
//Call the IsMatch method of the Regex class, as shown in the following example.
// Instance method:

Regex reg = new Regex(@"(?:^|,)(?=[^"]|(")?)"?((?(1)[^"]*|[^,"]*))"?(?=,|$)");
Response.Write(reg.IsMatch(your input string value));

// Static method:
if (!Regex.IsMatch(Your input string value, 
                   @"(?:^|,)(?=[^"]|(")?)"?((?(1)[^"]*|[^,"]*))"?(?=,|$)"))
{
  // Name does not match schema
}

Open in new window

for more reference please visit the below link
How To: Use Regular Expressions to Constrain Input in ASP.NET
hope this helps.
Just a note... Often times, the issue with converting a regular expression from one language's syntax to another lies in the ability/inability to explain what a given regular expression is meant to do. You may use tools like https://regex101.com/ or Expresso (http://www.ultrapico.com/expresso.htm) help with that, which goes a long way with assisting a conversion effort.
Avatar of jazzIIIlove

ASKER

Jitendra Patil;

Your java pattern is giving errors in the editor for quotes. Can you check?

Thanks Mlanda.
in which editor you are checking this, and what is the input you are checking for , can you please provide the same?
I am checking it intellij
ASKER CERTIFIED SOLUTION
Avatar of jazzIIIlove
jazzIIIlove
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
this is the one.

If someone can break this, I will grant all the points.