0

This solution somewhat solves my issue but need to exclude any characters after Ordernr until a carriage return(new line)

  1. 21Sid1
  2. Ordernr
  3. E17222
  4. By
  5. Seller

I am using ordernr[\r\n]+([^\n\r]+) to match ordernr and E17222 above. I want to do the same for the case below:

  1. 21Sid1
  2. Ordernr Skip everything upto new line
  3. E17222
  4. By
  5. Seller

Basically exclude everything when Ordernr is found until a new line and grab the next line

Sidh Rg
  • 3
  • 1
  • 7

1 Answers1

2

This should do:

/Ordernr.*[\r\n]+(.*)/g

Since, .* matches everything except line terminators, so we're matching everything after Ordernr except new line using .* then a new line and again we capture evrything on the next line using (.*).

Live demo here

Here's a sample code in Java:

String line = "21Sid1\nOrdernr Skip everything upto new line\nE17222\nBy\nSeller";
Pattern pattern = Pattern.compile("Ordernr.*[\r\n]+(.*)");
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
    System.out.println("group 1: " + matcher.group(1));
}else{
    System.out.println("No match found");
}

OUTPUT:

group 1: E17222

Check the result here.

EDIT

To make this regex work with your dot matches all consraint, try this:

/Ordernr[^\n\r]*[\r\n]+([^\n\r]*)/g

here I've replaced dot to match everything except new lines.

Ashish Ranjan
  • 5,523
  • 2
  • 18
  • 39
  • I want to match Ordernr and skip everything till the line terminator and capture everything on the next line. – Currently it matches everything on Ordernr line. Could we just match Ordernr and Skip everything upto new line. – Sidh Rg Oct 17 '17 at 21:50
  • it's not matching everything, there's a thing called `capturing` in regex, checkout the code and it's output in java : https://ideone.com/95gDGR – Ashish Ranjan Oct 18 '17 at 04:18
  • So this is what is happening. I am trying to use it in one of the field processors for JIRA. It works when I try it on live demo sites for both JAVA and JavaScript. However when I try it on the plugin it captures the last line. The reason it is doing that is because of Dot Matches all usage. So basically .* matches all the way to the end and captures the last line instead of next line. Any way to get around this? Here's the screenshot [link](https://pasteboard.co/GPtu179.png) – Sidh Rg Oct 18 '17 at 08:42
  • what plugin are you talking about? – Ashish Ranjan Oct 18 '17 at 09:13
  • It's JEMH for JIRA which uses [link](http://regexpal.com.s3-website-us-east-1.amazonaws.com/) for regex. – Sidh Rg Oct 18 '17 at 09:18
  • check the updated answer, it is capturing exactly what you want on your regex tester. – Ashish Ranjan Oct 18 '17 at 09:20
  • Awesome! Thanks ! – Sidh Rg Oct 18 '17 at 09:38
  • mark the answer as correct if it solves the issue, it helps future visitors find solution to their problem. – Ashish Ranjan Oct 18 '17 at 09:45
  • Hey Ashish, needed a minor update - if I want to capture till a # instead of next line how would I do that? – Sidh Rg Oct 19 '17 at 07:15
  • `Ordernr[^\n\r]*[\r\n]+([^\n\r#]*)` i.e, just add a `#` to the last set – Ashish Ranjan Oct 19 '17 at 07:17
  • Great ! Thank you ! – Sidh Rg Oct 19 '17 at 07:24
  • Hi Ashish, Sorry to chime back in with another request - here's another scenario I would like to capture - I have done some of it but couldn't finish it - would you be able to help>? - I want to capture Date format - yyyy/mm/dd hh:mm **#Date [^\n\r]*[\r\n]+([12]\d{3}/(0[1-9]|1[0-2])/(0[1-9]|[12]\d|3[01]))** This expression captures the next line until the day but I wanted to include time part as well and also be able to capture single digits for month and day instead of having to enter two digits - user could enter 5 for month instead of 05 and same for the day. – Sidh Rg Oct 23 '17 at 14:22
  • it would great if you could post it as a new question @SidhRg – Ashish Ranjan Oct 23 '17 at 15:30