Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Region named capture (-1--1) #41

Open
davibaldin opened this issue Apr 16, 2019 · 0 comments
Open

Region named capture (-1--1) #41

davibaldin opened this issue Apr 16, 2019 · 0 comments

Comments

@davibaldin
Copy link

davibaldin commented Apr 16, 2019

Hi,

Can you give me some feedback on this issue?

I'm trying to mach Named capture groups in a multiline byte[] content and getting a -1 -1 index range for group2 for pattern (A).

Pattern A is: (?[0-9.]{1,5}%)|(?dev = .*)

However, pattern (B) works fine for non multiline (\n) content.

Pattern B is: (?[0-9.]{1,5}%).*(?dev = .*)

Debug regex on: https://regex101.com/r/y2ER1a/1

Content (with multiline) is:
Content >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: icmp_seq=0 ttl=57 time=12.934 ms
64 bytes from 8.8.8.8: icmp_seq=1 ttl=57 time=13.145 ms

--- 8.8.8.8 ping statistics ---
2 packets transmitted, 2 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 12.934/13.040/13.145/0.106 ms
Content <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

My debug output is:

result = 226
D region Region:
0: (226-230) 1: (226-230) 2: (-1--1)
D nameEntry loss 1
loss -> 226, 230
0.0%
D nameEntry rtt 2
java.lang.StringIndexOutOfBoundsException: String index out of range: -1

My code is:

public class RegexOutputPluginHandler {

private byte[] patternBytes;
private Regex regex = null;


public RegexOutputPluginHandler() {
	String pattern = "(?<loss>[0-9\\.]{1,5}%)|(?<rtt>dev = .*)";
	
	if (pattern != null) {
		this.patternBytes = pattern.getBytes();
		this.regex = new Regex(this.patternBytes, 0, this.patternBytes.length, Option.MULTILINE, UTF8Encoding.INSTANCE);
	}
	
}


public Map<String, Object> extract(byte[] content) {
	
	System.err.println("D content " + content);

	if (content == null) {
		return null;
	}
	
	System.err.println("D content len " + content.length);

	Map<String, Object> fields = new HashMap<String, Object>();

	Matcher matcher = regex.matcher(content);
	int result = matcher.search(0, content.length, Option.MULTILINE);

	System.out.println("result = " + result);

if (result != -1) {
		Region region = matcher.getEagerRegion();
		
		System.out.println("D region " + region.toString());
		
		for (Iterator<NameEntry> entry = regex.namedBackrefIterator(); entry.hasNext();) {
			NameEntry e = entry.next();
			
			System.out.println("D nameEntry " + e.toString());
			
			int number = e.getBackRefs()[0]; // can have many refs per name
			int begin = region.beg[number];
			int end = region.end[number];

			String fieldName = new String(e.name, e.nameP, e.nameEnd - e.nameP);
			String fieldContent = new String(content, begin, end - begin);


			System.out.println(fieldName + " -> " + begin + ", " + end);
			System.out.println(fieldContent);

		}
	}else {
		System.err.println("D matcher none");
	}
	
	return fields;
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant