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
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 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 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();
}
This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 6 years ago.
I am using "^[(\+[0-9]{1,3}\.[0-9]{4,14}(?:x.+)?]$" regex to validate phone number. I want it to work for international numbers as well.
It is working for the patterns:
+4454475294x364
I want to add space and '-' also.
example: +44 544-75294 x364.
What changes I need more in my regex.
Thanks.
Description
You provided the following examples of numbers you'd like matched.
+4454475294x364
+44 544-75294 x364
(123) 555-1212x4567
123-555-1232
The Regex
This regex will do the following:
Match international numbers of the format you provided
Match North American numbers
If the phone number is followed by an extension, then capture that
Allow spaces, hyphens, and parentheses at obvious spots
This is limited to just the formats that you listed in your question
^(?:[+][0-9]{2}\s?[0-9]{3}[-]?[0-9]{3,}|(?:[(][0-9]{3}[)]|[0-9]{3})\s*[-]?\s*[0-9]{3}[-][0-9]{4})(?:\s*x\s*[0-9]+)?
Note: for Java you'll need to escape the forward slashes \ to look like \\.
Explanation
NODE EXPLANATION
----------------------------------------------------------------------
^ the beginning of a "line"
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
[+] any character of: '+'
----------------------------------------------------------------------
[0-9]{2} any character of: '0' to '9' (2 times)
----------------------------------------------------------------------
\s? whitespace (\n, \r, \t, \f, and " ")
(optional (matching the most amount
possible))
----------------------------------------------------------------------
[0-9]{3} any character of: '0' to '9' (3 times)
----------------------------------------------------------------------
[-]? any character of: '-' (optional
(matching the most amount possible))
----------------------------------------------------------------------
[0-9]{3,} any character of: '0' to '9' (at least 3
times (matching the most amount
possible))
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
[(] any character of: '('
----------------------------------------------------------------------
[0-9]{3} any character of: '0' to '9' (3 times)
----------------------------------------------------------------------
[)] any character of: ')'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[0-9]{3} any character of: '0' to '9' (3 times)
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
----------------------------------------------------------------------
[-]? any character of: '-' (optional
(matching the most amount possible))
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
----------------------------------------------------------------------
[0-9]{3} any character of: '0' to '9' (3 times)
----------------------------------------------------------------------
[-] any character of: '-'
----------------------------------------------------------------------
[0-9]{4} any character of: '0' to '9' (4 times)
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
----------------------------------------------------------------------
x 'x'
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
----------------------------------------------------------------------
[0-9]+ any character of: '0' to '9' (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
)? end of grouping
Examples
Using the sample text above
Matches
[0][0] = +4454475294x364
[1][0] = +44 544-75294 x364
[2][0] = (123) 555-1212x4567
[3][0] = 123-555-1232