I want to pick up the entire block from the starting title to the end title, but not include the end title. Example is :
<section1>
Base_Currency=EUR
Description=Revaluation
Grouping_File
<section2>
the match result should be:
<section1>
Base_Currency=EUR
Description=Revaluation
Grouping_File
Problem is that how can I formulate the Pattern for this match using Regex in java?
If your input is something like below
<section1>
Base_Currency=EUR
Description=Revaluation
Grouping_File
<section2>
Base_Currency=EUR
Description=Revaluation
Grouping_File
<section3>
Base_Currency=EUR
Description=Revaluation
Grouping_File
Then you can use the following regex
(?s)(<section\d+>.*?)(?=<section\d+>|$)
Explanation for the regex is
NODE EXPLANATION
--------------------------------------------------------------------------------
(?s) set flags for this block (with . matching
\n) (case-sensitive) (with ^ and $
matching normally) (matching whitespace
and # normally)
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
<section '<section'
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
> '>'
--------------------------------------------------------------------------------
.*? any character (0 or more times (matching
the least amount possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
<section '<section'
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
> '>'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
$ before an optional \n, and the end of
the string
--------------------------------------------------------------------------------
) end of look-ahead
If you want to match only for one tag then you can use
(?s)(<section\d+>[^<]*)
Explanation for this regex is
NODE EXPLANATION
--------------------------------------------------------------------------------
(?s) set flags for this block (with . matching
\n) (case-sensitive) (with ^ and $
matching normally) (matching whitespace
and # normally)
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
<section '<section'
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
> '>'
--------------------------------------------------------------------------------
[^<]* any character except: '<' (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \1
If your entire input is of this format, you can simply split:
String[] sections = input.split("\\R(?=<)");
\R is "any newline sequence" and (?=<) means "the next char is a '<'".
However if that's not the case, from the regex toolbox you're going to need:
the DOTALL flag so dot matches newlines too
the MULTILINE flag so ^ matches start of line too
a negative look ahead so you stop consuming at the start of the next section
Assuming "sections" start with a "<" at the start of a line:
"(?sm)^<\\w+>(.(?!^<))*"
Here's how you could use it:
String input = "<section1>\nBase_Currency=EUR\nDescription=Revaluation\nGrouping_File\n<section2>\nfoo";
Matcher matcher = Pattern.compile("(?sm)^<\\w+>(.(?!^<))*").matcher(input);
while (matcher.find()) {
String section = matcher.group();
}
Related
Regex expression: [A-Z]([^0-9]|[^A-Z])+[A-Z]
The requirements are that the string should start and end with a capital letter A-Z, and contain at least one number in between. It should not have anything else besides capital letters on the inside. However, it's accepting spaces and punctuation too.
My expression fails the following test case A65AJ3L 3F,D due to the comma and whitespace.
Why does this happen when I explicitly said only numbers and uppercase letters can be in the string?
Starting the character class with [^ makes is a negated character class.
Using ([^0-9]|[^A-Z])+ matches any char except a digit (but does match A-Z), or any char except A-Z (but does match a digit).
This way it can match any character.
If you would turn it into [A-Z]([0-9]|[A-Z])+[A-Z] it still does not make it mandatory to match at least a single digit on the inside due to the alternation | and it can still match AAA for example.
You might use:
^[A-Z]+[0-9][A-Z0-9]*[A-Z]$
The pattern matches:
^ Start of string
[A-Z]+ Match 1+ times A-Z
[0-9] Match a single digit
[A-Z0-9]* Optionally match either A-Z or 0-9
[A-Z] Match a single char A-Z
$ End of string
Regex demo
Use
^(?=\D*\d\D*$)[A-Z][A-Z\d]*[A-Z]$
See regex proof.
(?=\D*\d\D*$) requires only one digit in the string, no more no less.
EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
\D* non-digits (all but 0-9) (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
\D* non-digits (all but 0-9) (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of
the string
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
[A-Z] any character of: 'A' to 'Z'
--------------------------------------------------------------------------------
[A-Z\d]* any character of: 'A' to 'Z', digits (0-9)
(0 or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
[A-Z] any character of: 'A' to 'Z'
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
I have a text which will have multiple record information. All the records will begin with same regex pattern and each record will have some unique text. Here I want to fetch only the entry name and value of the record which contains the text "Entertainment Extra 4K". I tried to with a regex and but as the regex begn match to first one I'm always getting the first record values.
https://regex101.com/r/MAAc1s/1
In the above link, I'm want to get only the below record info,
<input type='radio' class="radio" id="bb_radio128411" name='484' value='13'
-----
----Entertainment Extra 4K
Any suggestions would be really appreciated
Use
name='(\d+)'\s+value='(\d+)'[^<>]*Entertainment Extra 4K
See proof.
EXPLANATION
--------------------------------------------------------------------------------
name=' 'name=\''
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
' '\''
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
value=' 'value=\''
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
) end of \2
--------------------------------------------------------------------------------
' '\''
--------------------------------------------------------------------------------
[^<>]* any character except: '<', '>' (0 or more
times (matching the most amount possible))
--------------------------------------------------------------------------------
Entertainment Extra 'Entertainment Extra 4K'
4K
I want to parse the column data and extract the required column info based on regex. Below shared is link that I tried,
^\s*(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\s*(\d+)\s*(\d+)
https://regex101.com/r/lwzfQA/1
From that above link, I want to parse onlu the three rows data but it is matching with other details like "Sat Jan 30 15:56:06.144 UTC". I see first two matches in the above link are not proper but last two looks fine. which regex that I can use to parse only column info.
In your example data, the columns are separated by more than 1 whitespace, but in your pattern you make those spaces optional using \s* Also note that \S matches a non whitespace character which is a broad match.
As you tagged Java, I would suggest making use of \h{2,} to match 2 or more horizontal whitespace chars as \s can also match a newline and might give unexpected results.
You could also add an anchor $ to assert the end of the string to prevent partial matches.
^\h{2,}(\S+)\h{2,}(\S+)\h{2,}(\S+)\h{2,}(\S+)\h{2,}(\d+)\h{2,}(\d+)$
Regex demo
In Java with the doubled backslashes
String regex = "^\\h{2,}(\\S+)\\h{2,}(\\S+)\\h{2,}(\\S+)\\h{2,}(\\S+)\\h{2,}(\\d+)\\h{2,}(\\d+)$";
Try to replace your first "*" by a "+".
What it means:
"*" = 0 or more
"+" = 1 or more
Given the fact that all your columns begin with some spaces, it excludes the date line which does not begin with a space.
^\s+(\S+)\s*(\S+)\s*(\S+)\s*(\S+)\s*(\d+)\s*(\d+)
As Peter mentioned, your regex will function if using the + (one or more) operator on your space matching at the beginning instead of * (zero or more).
I would further encourage you to recognize that this is a "Fixed Width" format table, meaning each of the columns are simply padded with spaces to a predetermined width. If you will be parsing a large file this way, you will find it much more predictable and easy to debug by using regex to match the line of all hyphens to chop of the beginning, then going line by line with simple substrings and trim at the column length for each column.
If you wish to continue using regex for this, you could also explore other range quantifiers and named groups. This would make the regex a little more clear and help identify issues with formatting later. Please see the following example:
https://regex101.com/r/KEnutx/1
The (?<name>\d+), for example, names the capture group. In many languages, you can then refer to the group by this name, easily pulling out your data and not making your code specific to the index of the groups. Also, it is much easier to find that name when you are debugging or improving your regex to accomodate changes.
You can try changing your first whitespace filter from ^\s* to ^\s+. The effectively filters out the date line since it does not begin with whitespace. Also, if possible, it might be helpful to change the filters to be more specific to the data your searching. For example, with "BE100" you could use \D+\d+, or something even more specific depending on the data.
\s matches line breaks, exclude them from \s with [^\S\r\n] and use + instead of *:
^[^\S\r\n]+(\S+)[^\S\r\n]+(\S+)[^\S\r\n]+(\S+)[^\S\r\n]+(\S+)[^\S\r\n]+(\d+)[^\S\r\n]+(\d+)
See proof
Explanation
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
[^\S\r\n]+ any character except: non-whitespace (all
but \n, \r, \t, \f, and " "), '\r'
(carriage return), '\n' (newline) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
\S+ non-whitespace (all but \n, \r, \t, \f,
and " ") (1 or more times (matching the
most amount possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
[^\S\r\n]+ any character except: non-whitespace (all
but \n, \r, \t, \f, and " "), '\r'
(carriage return), '\n' (newline) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
\S+ non-whitespace (all but \n, \r, \t, \f,
and " ") (1 or more times (matching the
most amount possible))
--------------------------------------------------------------------------------
) end of \2
--------------------------------------------------------------------------------
[^\S\r\n]+ any character except: non-whitespace (all
but \n, \r, \t, \f, and " "), '\r'
(carriage return), '\n' (newline) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \3:
--------------------------------------------------------------------------------
\S+ non-whitespace (all but \n, \r, \t, \f,
and " ") (1 or more times (matching the
most amount possible))
--------------------------------------------------------------------------------
) end of \3
--------------------------------------------------------------------------------
[^\S\r\n]+ any character except: non-whitespace (all
but \n, \r, \t, \f, and " "), '\r'
(carriage return), '\n' (newline) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \4:
--------------------------------------------------------------------------------
\S+ non-whitespace (all but \n, \r, \t, \f,
and " ") (1 or more times (matching the
most amount possible))
--------------------------------------------------------------------------------
) end of \4
--------------------------------------------------------------------------------
[^\S\r\n]+ any character except: non-whitespace (all
but \n, \r, \t, \f, and " "), '\r'
(carriage return), '\n' (newline) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \5:
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
) end of \5
--------------------------------------------------------------------------------
[^\S\r\n]+ any character except: non-whitespace (all
but \n, \r, \t, \f, and " "), '\r'
(carriage return), '\n' (newline) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \6:
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
) end of \6
I have trouble fetching Key Value pairs with my regex
Code so far:
String raw = '''
MA1
D. Mueller Gießer
MA2 Peter
Mustermann 2. Mann
MA3 Ulrike Mastorius Schmelzer
MA4 Heiner Becker
s 3.Mann
MA5 Rudolf Peters
Gießer
'''
Map map = [:]
ArrayList<String> split = raw.findAll("(MA\\d)+(.*)"){ full, name, value -> map[name] = value }
println map
Output is:
[MA1:, MA2: Peter, MA3: Ulrike Mastorius Schmelzer, MA4: Heiner Becker, MA5: Rudolf Peters]
In my case the keys are:
MA1, MA2, MA3, MA\d (so MA with any 1 digit Number)
The value is absolutely everything until the next key comes up (including line breaks, tab, spaces etc...)
Does anybody have a clue how to do this?
Thanks in advance,
Sebastian
You can capture in the second group all that follows after the key and all the lines that do not start with the key
^(MA\d+)(.*(?:\R(?!MA\d).*)*)
The pattern matches
^ Start of string
(MA\d+) Capture group 1 matching MA and 1+ digits
( Capture group 2
.* Match the rest of the line
(?:\R(?!MA\d).*)* Match all lines that do not start with MA followed by a digit, where \R matches any unicode newline sequence
) Close group 2
Regex demo
In Java with the doubled escaped backslashes
final String regex = "^(MA\\d+)(.*(?:\\R(?!MA\\d).*)*)";
Use
(?ms)^(MA\d+)(.*?)(?=\nMA\d|\z)
See proof.
Explanation
EXPLANATION
--------------------------------------------------------------------------------
(?ms) set flags for this block (with ^ and $
matching start and end of line) (with .
matching \n) (case-sensitive) (matching
whitespace and # normally)
--------------------------------------------------------------------------------
^ the beginning of a "line"
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
MA 'MA'
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
.*? any character (0 or more times (matching
the least amount possible))
--------------------------------------------------------------------------------
) end of \2
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
\n '\n' (newline)
--------------------------------------------------------------------------------
MA 'MA'
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
\z the end of the string
--------------------------------------------------------------------------------
) end of look-ahead
I have this:
110121 NATURAL 95 1570,40
110121 NATURAL 95 1570,40*
41,110 1 x 38,20 CZK)[A] *
' 31,831 261,791 1308,61)
>01572 PRAVO SO 17,00
1,000 ks x 17,00
1570,40
Every line of this output is saved in List and I want to get number 1570,40
My regular expressions looks like this for this type of format
"([1-9][0-9]*[\\.|,][0-9]{2})[^\\.\\d](.*)"
"^([1-9][0-9]*[\\.|,][0-9]{2})$"
I have a problem that 1570,40 at the last line if founded (by second regular expression), also 1570,40 (from line with 1570,40* at the end) but the first line is not founded.. do you know where is the problem?
Not sure I well understand your needs, but I think you could use word boundaries like:
\b([1-9]\d*[.,]\d{2})\b
In order to not match dates, you can use:
(?:^|[^.,\d])(\d+[,.]\d\d)(?:[^.,\d]|$)
explanation:
The regular expression:
(?-imsx:(?:^|[^.,\d])(\d+[,.]\d\d)(?:[^.,\d]|$))
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[^.,\d] any character except: '.', ',', digits
(0-9)
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
----------------------------------------------------------------------
[,.] any character of: ',', '.'
----------------------------------------------------------------------
\d digits (0-9)
----------------------------------------------------------------------
\d digits (0-9)
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
[^.,\d] any character except: '.', ',', digits
(0-9)
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
$ before an optional \n, and the end of
the string
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
The "([1-9][0-9]*[\\.|,][0-9]{2})[^\\.\\d](.*)" has [^\\.\\d], it means it expects one non-digit, non-dot symbol right after the number. The second line has * which matches it. First line has the number at the end of line, so nothing matches. I think you need just one regexp which will catch all numbers: [^.\\d]*([1-9][0-9]*[.,][0-9]{2})[^.\\d]*. Also, you should use find instead of match to find any substring in a string instead of matching the whole string. Also, maybe it has a sense to find all matches in case if a line has two such numbers in it, not sure if it is a case for you or not.
Also, use either [0-9] or \d. At the moment it is confusing - it means the same, but looks differently.
Try this:
String s = "41,110 1 x 38,20 CZK)[A] * ";
Matcher m = Pattern.compile("\\d+,\\d+").matcher(s);
while(m.find()) {
System.out.println(m.group());
}