Repeating regular expressionIs there a regular expression to detect a valid regular expression?jQuery selector regular expressionsHow to validate an email address using a regular expression?Regular Expression for alphanumeric and underscoresRegular expression to match a line that doesn't contain a wordHow do you access the matched groups in a JavaScript regular expression?Regular Expressions: Is there an AND operator?How do you use a variable in a regular expression?Java Stringparsing with RegexpIterating through the regex find

French license plates

How do we know neutrons have no charge?

Earliest time frog can jump to the other side of a river in C#. Codility's task

How many stack cables would be needed if we want to stack two 3850 switches

Would an object shot from earth fall into the sun?

Why most footers have a background color has a divider of section?

Create the same subfolders in another folder

Would allowing versatile weapons wielded in two hands to benefit from Dueling be unbalanced?

Would a horse be sufficient buffer to prevent injury when falling from a great height?

How to bring home documents from work?

How to export all graphics from a notebook?

Is it possible to take a database offline when doing a backup using an SQL job?

Sci-fi movie with one survivor and an organism(?) recreating his memories

Why do Russians sometimes spell "жирный" (fatty) as "жырный"?

Can a passenger predict that an airline is about to go bankrupt?

Is the "spacetime" the same thing as the mathematical 4th dimension?

Integrals from Brasilian Math Olympiad 2019

How do I introduce dark themes?

How many space launch vehicles are under development worldwide?

Is it mandatory to use contractions in tag questions and the like?

How big would the ice ball have to be to deliver all the water at once?

As a team leader is it appropriate to bring in fundraiser candy?

How to compare integers in Tex?

Would a 737 pilot use flaps in nose dive?



Repeating regular expression


Is there a regular expression to detect a valid regular expression?jQuery selector regular expressionsHow to validate an email address using a regular expression?Regular Expression for alphanumeric and underscoresRegular expression to match a line that doesn't contain a wordHow do you access the matched groups in a JavaScript regular expression?Regular Expressions: Is there an AND operator?How do you use a variable in a regular expression?Java Stringparsing with RegexpIterating through the regex find






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have a log file which i want to parse. It is about getting the Values between the square brackets and after the "OK:" using regex.
The Problem is i do not know how many times the pattern is occuring and i can not say how long each code is. So i can only relay on the fact that it is surrounded by "[OK:" and "]".



So far i tried to use this pattern here as regex:



String ok_pattern = "(.*itId=<)(.1,10)(>.*)(\[OK:)(.4,27)(].*)";
Pattern p_ok = Pattern.compile(ok_pattern);

String testString = "RANDOMTEXT itId=<1232> Code < [OK:AZ1000105] [OK:10000006] [OK:F1000000007] > RANDOMTEXT";

Matcher m = p_ok.matcher(testString);
if(m.find())
System.out.println(m.group(5));




But this only works for the case when there is only one "[OK:...]".
I played around with using a "*" and "+" after the 5th group but i could not succeed.
How do i do this repetetive and still capture all results?



My goal is to extract the itemId and the (char-)number combination after the "OK:" using regex. So in this example I want to get "1232"(ItemID) and "AZ1000105", "10000006", "F1000000007".



I am thankful for every help!










share|improve this question


























  • You could use a capturing group [OK:([A-Z0-9]+)] regex101.com/r/rty1K8/1

    – The fourth bird
    Mar 28 at 15:43












  • Do you want to capture the id too ?

    – Cid
    Mar 28 at 15:44











  • @FILE_q Do you mean like this? regex101.com/r/rty1K8/2

    – The fourth bird
    Mar 28 at 16:08

















1















I have a log file which i want to parse. It is about getting the Values between the square brackets and after the "OK:" using regex.
The Problem is i do not know how many times the pattern is occuring and i can not say how long each code is. So i can only relay on the fact that it is surrounded by "[OK:" and "]".



So far i tried to use this pattern here as regex:



String ok_pattern = "(.*itId=<)(.1,10)(>.*)(\[OK:)(.4,27)(].*)";
Pattern p_ok = Pattern.compile(ok_pattern);

String testString = "RANDOMTEXT itId=<1232> Code < [OK:AZ1000105] [OK:10000006] [OK:F1000000007] > RANDOMTEXT";

Matcher m = p_ok.matcher(testString);
if(m.find())
System.out.println(m.group(5));




But this only works for the case when there is only one "[OK:...]".
I played around with using a "*" and "+" after the 5th group but i could not succeed.
How do i do this repetetive and still capture all results?



My goal is to extract the itemId and the (char-)number combination after the "OK:" using regex. So in this example I want to get "1232"(ItemID) and "AZ1000105", "10000006", "F1000000007".



I am thankful for every help!










share|improve this question


























  • You could use a capturing group [OK:([A-Z0-9]+)] regex101.com/r/rty1K8/1

    – The fourth bird
    Mar 28 at 15:43












  • Do you want to capture the id too ?

    – Cid
    Mar 28 at 15:44











  • @FILE_q Do you mean like this? regex101.com/r/rty1K8/2

    – The fourth bird
    Mar 28 at 16:08













1












1








1








I have a log file which i want to parse. It is about getting the Values between the square brackets and after the "OK:" using regex.
The Problem is i do not know how many times the pattern is occuring and i can not say how long each code is. So i can only relay on the fact that it is surrounded by "[OK:" and "]".



So far i tried to use this pattern here as regex:



String ok_pattern = "(.*itId=<)(.1,10)(>.*)(\[OK:)(.4,27)(].*)";
Pattern p_ok = Pattern.compile(ok_pattern);

String testString = "RANDOMTEXT itId=<1232> Code < [OK:AZ1000105] [OK:10000006] [OK:F1000000007] > RANDOMTEXT";

Matcher m = p_ok.matcher(testString);
if(m.find())
System.out.println(m.group(5));




But this only works for the case when there is only one "[OK:...]".
I played around with using a "*" and "+" after the 5th group but i could not succeed.
How do i do this repetetive and still capture all results?



My goal is to extract the itemId and the (char-)number combination after the "OK:" using regex. So in this example I want to get "1232"(ItemID) and "AZ1000105", "10000006", "F1000000007".



I am thankful for every help!










share|improve this question
















I have a log file which i want to parse. It is about getting the Values between the square brackets and after the "OK:" using regex.
The Problem is i do not know how many times the pattern is occuring and i can not say how long each code is. So i can only relay on the fact that it is surrounded by "[OK:" and "]".



So far i tried to use this pattern here as regex:



String ok_pattern = "(.*itId=<)(.1,10)(>.*)(\[OK:)(.4,27)(].*)";
Pattern p_ok = Pattern.compile(ok_pattern);

String testString = "RANDOMTEXT itId=<1232> Code < [OK:AZ1000105] [OK:10000006] [OK:F1000000007] > RANDOMTEXT";

Matcher m = p_ok.matcher(testString);
if(m.find())
System.out.println(m.group(5));




But this only works for the case when there is only one "[OK:...]".
I played around with using a "*" and "+" after the 5th group but i could not succeed.
How do i do this repetetive and still capture all results?



My goal is to extract the itemId and the (char-)number combination after the "OK:" using regex. So in this example I want to get "1232"(ItemID) and "AZ1000105", "10000006", "F1000000007".



I am thankful for every help!







java regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 15:56







FILO_q

















asked Mar 28 at 15:39









FILO_qFILO_q

83 bronze badges




83 bronze badges















  • You could use a capturing group [OK:([A-Z0-9]+)] regex101.com/r/rty1K8/1

    – The fourth bird
    Mar 28 at 15:43












  • Do you want to capture the id too ?

    – Cid
    Mar 28 at 15:44











  • @FILE_q Do you mean like this? regex101.com/r/rty1K8/2

    – The fourth bird
    Mar 28 at 16:08

















  • You could use a capturing group [OK:([A-Z0-9]+)] regex101.com/r/rty1K8/1

    – The fourth bird
    Mar 28 at 15:43












  • Do you want to capture the id too ?

    – Cid
    Mar 28 at 15:44











  • @FILE_q Do you mean like this? regex101.com/r/rty1K8/2

    – The fourth bird
    Mar 28 at 16:08
















You could use a capturing group [OK:([A-Z0-9]+)] regex101.com/r/rty1K8/1

– The fourth bird
Mar 28 at 15:43






You could use a capturing group [OK:([A-Z0-9]+)] regex101.com/r/rty1K8/1

– The fourth bird
Mar 28 at 15:43














Do you want to capture the id too ?

– Cid
Mar 28 at 15:44





Do you want to capture the id too ?

– Cid
Mar 28 at 15:44













@FILE_q Do you mean like this? regex101.com/r/rty1K8/2

– The fourth bird
Mar 28 at 16:08





@FILE_q Do you mean like this? regex101.com/r/rty1K8/2

– The fourth bird
Mar 28 at 16:08












2 Answers
2






active

oldest

votes


















2
















Your basic setup is correct, but your pattern is somewhat off from ideal. Try using the following regex pattern:



(?<=[OK:)[^]]+|(?<=itId=<)[^>]+


This still uses a lookbehind, but it only asserts that what precedes is [OK:. Then, it matches, without even using a capture group, any amount of characters which are not a closing square bracket. This corresponds to the content you are trying to find. The portion to the right of the alternation matches itId values.



String ok_pattern = "(?<=\[OK:)[^\]]+|(?<=itId=<)[^>]+";
Pattern p_ok = Pattern.compile(ok_pattern);
String testString = "RANDOMTEXT itId=<1232> Code < [OK:AZ1000105] [OK:10000006] [OK:F1000000007] > RANDOMTEXT";

Matcher m = p_ok.matcher(testString);
while (m.find())
System.out.println(m.group(0));


1232
AZ1000105
10000006
F1000000007





share|improve this answer



























  • following that approach helped me to actually get the codes. but then i could not add the "(.*ItemId=<)" in front of it anymore. how can i combine getting the one ItemId before and then the OK-codes?

    – FILO_q
    Mar 28 at 15:53












  • @FILO_q Not sure what more I can say here; my code works with your sample input. If you have other input cases, then edit your question or ask a new one.

    – Tim Biegeleisen
    Mar 28 at 15:54












  • ok so summarized: i want to get the itId and then afterwards the unknown number of OK: codes.

    – FILO_q
    Mar 28 at 15:59











  • @FILO_q I edited my question to also capture Id's using an alternation. Both Ids and codes get printed, so hopefully the output would be clear.

    – Tim Biegeleisen
    Mar 28 at 16:05












  • that is very close, but it returns now "itId=<1232>" instead of only "1232". If you can tell me how to fix it im done!

    – FILO_q
    Mar 28 at 16:19



















0
















If you want to capture the digits in itId=<1232> followed by subsequent captures of what is after OK: in that order , you could make use of the G anchor to assert the position at the end of the previous match.



Match the itId digits in the first capturing group and the value of OK: in the second capturing group:



itId=<(d+)> Code < |G(?!^)[OK:([A-Z0-9]+)]s*


In Java:



String ok_pattern = "itId=<(\d+)> Code < |\G(?!^)\[OK:([A-Z0-9]+)\]\s*";


Explanation




  • itId=<(d+)> Code < Match the first part and capture 1+ digits in group 1


  • | Or


  • G(?!^) End of the previous match, not at the start


  • [OK:([A-Z0-9]+)]s* Match [OK:, then capture your value in group 2 and match ] followed by 0+ whitespace chars

Regex demo | Java demo



Note that if you want to match more than ([A-Z0-9]+) you could also use a negated character class to match not a square bracket ([^]]+)



For example, you might check for the existence of the groups:



String ok_pattern = "itId=<(\d+)> Code < |\G(?!^)\[OK:([^]]+)\]\s*";
Pattern p_ok = Pattern.compile(ok_pattern);
String testString = "RANDOMTEXT itId=<1232> Code < [OK:AZ1000105] [OK:10000006] [OK:F1000000007] > RANDOMTEXT";
Matcher m = p_ok.matcher(testString);

while(m.find())
if (null != m.group(1))
System.out.println("itId: " + m.group(1));

if (null != m.group(2))
System.out.println("Ok code: " + m.group(2));







share|improve this answer



























  • how can i actually access the values then? if i try to use: ``` Matcher m = p_ok.matcher(testString); if(m.find()) System.out.println(m.group(5)); ``` I dont get any results.

    – FILO_q
    Mar 28 at 16:21












  • Did you check the demo link? For exmple ideone.com/QgveK8 You can check for the existence of the different groups.

    – The fourth bird
    Mar 28 at 16:22











  • my network actually blocks the java demo and i cant access it...

    – FILO_q
    Mar 28 at 16:25











  • @FILO_q I have added and example and perhaps test it here rextester.com/ZTEMBP76897

    – The fourth bird
    Mar 28 at 16:32






  • 1





    Thank you very much. You and Tim Biegeleisen solved it at the same time... thanks for your help!

    – FILO_q
    Mar 28 at 16:35













Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);














draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55401617%2frepeating-regular-expression%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2
















Your basic setup is correct, but your pattern is somewhat off from ideal. Try using the following regex pattern:



(?<=[OK:)[^]]+|(?<=itId=<)[^>]+


This still uses a lookbehind, but it only asserts that what precedes is [OK:. Then, it matches, without even using a capture group, any amount of characters which are not a closing square bracket. This corresponds to the content you are trying to find. The portion to the right of the alternation matches itId values.



String ok_pattern = "(?<=\[OK:)[^\]]+|(?<=itId=<)[^>]+";
Pattern p_ok = Pattern.compile(ok_pattern);
String testString = "RANDOMTEXT itId=<1232> Code < [OK:AZ1000105] [OK:10000006] [OK:F1000000007] > RANDOMTEXT";

Matcher m = p_ok.matcher(testString);
while (m.find())
System.out.println(m.group(0));


1232
AZ1000105
10000006
F1000000007





share|improve this answer



























  • following that approach helped me to actually get the codes. but then i could not add the "(.*ItemId=<)" in front of it anymore. how can i combine getting the one ItemId before and then the OK-codes?

    – FILO_q
    Mar 28 at 15:53












  • @FILO_q Not sure what more I can say here; my code works with your sample input. If you have other input cases, then edit your question or ask a new one.

    – Tim Biegeleisen
    Mar 28 at 15:54












  • ok so summarized: i want to get the itId and then afterwards the unknown number of OK: codes.

    – FILO_q
    Mar 28 at 15:59











  • @FILO_q I edited my question to also capture Id's using an alternation. Both Ids and codes get printed, so hopefully the output would be clear.

    – Tim Biegeleisen
    Mar 28 at 16:05












  • that is very close, but it returns now "itId=<1232>" instead of only "1232". If you can tell me how to fix it im done!

    – FILO_q
    Mar 28 at 16:19
















2
















Your basic setup is correct, but your pattern is somewhat off from ideal. Try using the following regex pattern:



(?<=[OK:)[^]]+|(?<=itId=<)[^>]+


This still uses a lookbehind, but it only asserts that what precedes is [OK:. Then, it matches, without even using a capture group, any amount of characters which are not a closing square bracket. This corresponds to the content you are trying to find. The portion to the right of the alternation matches itId values.



String ok_pattern = "(?<=\[OK:)[^\]]+|(?<=itId=<)[^>]+";
Pattern p_ok = Pattern.compile(ok_pattern);
String testString = "RANDOMTEXT itId=<1232> Code < [OK:AZ1000105] [OK:10000006] [OK:F1000000007] > RANDOMTEXT";

Matcher m = p_ok.matcher(testString);
while (m.find())
System.out.println(m.group(0));


1232
AZ1000105
10000006
F1000000007





share|improve this answer



























  • following that approach helped me to actually get the codes. but then i could not add the "(.*ItemId=<)" in front of it anymore. how can i combine getting the one ItemId before and then the OK-codes?

    – FILO_q
    Mar 28 at 15:53












  • @FILO_q Not sure what more I can say here; my code works with your sample input. If you have other input cases, then edit your question or ask a new one.

    – Tim Biegeleisen
    Mar 28 at 15:54












  • ok so summarized: i want to get the itId and then afterwards the unknown number of OK: codes.

    – FILO_q
    Mar 28 at 15:59











  • @FILO_q I edited my question to also capture Id's using an alternation. Both Ids and codes get printed, so hopefully the output would be clear.

    – Tim Biegeleisen
    Mar 28 at 16:05












  • that is very close, but it returns now "itId=<1232>" instead of only "1232". If you can tell me how to fix it im done!

    – FILO_q
    Mar 28 at 16:19














2














2










2









Your basic setup is correct, but your pattern is somewhat off from ideal. Try using the following regex pattern:



(?<=[OK:)[^]]+|(?<=itId=<)[^>]+


This still uses a lookbehind, but it only asserts that what precedes is [OK:. Then, it matches, without even using a capture group, any amount of characters which are not a closing square bracket. This corresponds to the content you are trying to find. The portion to the right of the alternation matches itId values.



String ok_pattern = "(?<=\[OK:)[^\]]+|(?<=itId=<)[^>]+";
Pattern p_ok = Pattern.compile(ok_pattern);
String testString = "RANDOMTEXT itId=<1232> Code < [OK:AZ1000105] [OK:10000006] [OK:F1000000007] > RANDOMTEXT";

Matcher m = p_ok.matcher(testString);
while (m.find())
System.out.println(m.group(0));


1232
AZ1000105
10000006
F1000000007





share|improve this answer















Your basic setup is correct, but your pattern is somewhat off from ideal. Try using the following regex pattern:



(?<=[OK:)[^]]+|(?<=itId=<)[^>]+


This still uses a lookbehind, but it only asserts that what precedes is [OK:. Then, it matches, without even using a capture group, any amount of characters which are not a closing square bracket. This corresponds to the content you are trying to find. The portion to the right of the alternation matches itId values.



String ok_pattern = "(?<=\[OK:)[^\]]+|(?<=itId=<)[^>]+";
Pattern p_ok = Pattern.compile(ok_pattern);
String testString = "RANDOMTEXT itId=<1232> Code < [OK:AZ1000105] [OK:10000006] [OK:F1000000007] > RANDOMTEXT";

Matcher m = p_ok.matcher(testString);
while (m.find())
System.out.println(m.group(0));


1232
AZ1000105
10000006
F1000000007






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 28 at 16:28

























answered Mar 28 at 15:43









Tim BiegeleisenTim Biegeleisen

274k14 gold badges122 silver badges185 bronze badges




274k14 gold badges122 silver badges185 bronze badges















  • following that approach helped me to actually get the codes. but then i could not add the "(.*ItemId=<)" in front of it anymore. how can i combine getting the one ItemId before and then the OK-codes?

    – FILO_q
    Mar 28 at 15:53












  • @FILO_q Not sure what more I can say here; my code works with your sample input. If you have other input cases, then edit your question or ask a new one.

    – Tim Biegeleisen
    Mar 28 at 15:54












  • ok so summarized: i want to get the itId and then afterwards the unknown number of OK: codes.

    – FILO_q
    Mar 28 at 15:59











  • @FILO_q I edited my question to also capture Id's using an alternation. Both Ids and codes get printed, so hopefully the output would be clear.

    – Tim Biegeleisen
    Mar 28 at 16:05












  • that is very close, but it returns now "itId=<1232>" instead of only "1232". If you can tell me how to fix it im done!

    – FILO_q
    Mar 28 at 16:19


















  • following that approach helped me to actually get the codes. but then i could not add the "(.*ItemId=<)" in front of it anymore. how can i combine getting the one ItemId before and then the OK-codes?

    – FILO_q
    Mar 28 at 15:53












  • @FILO_q Not sure what more I can say here; my code works with your sample input. If you have other input cases, then edit your question or ask a new one.

    – Tim Biegeleisen
    Mar 28 at 15:54












  • ok so summarized: i want to get the itId and then afterwards the unknown number of OK: codes.

    – FILO_q
    Mar 28 at 15:59











  • @FILO_q I edited my question to also capture Id's using an alternation. Both Ids and codes get printed, so hopefully the output would be clear.

    – Tim Biegeleisen
    Mar 28 at 16:05












  • that is very close, but it returns now "itId=<1232>" instead of only "1232". If you can tell me how to fix it im done!

    – FILO_q
    Mar 28 at 16:19

















following that approach helped me to actually get the codes. but then i could not add the "(.*ItemId=<)" in front of it anymore. how can i combine getting the one ItemId before and then the OK-codes?

– FILO_q
Mar 28 at 15:53






following that approach helped me to actually get the codes. but then i could not add the "(.*ItemId=<)" in front of it anymore. how can i combine getting the one ItemId before and then the OK-codes?

– FILO_q
Mar 28 at 15:53














@FILO_q Not sure what more I can say here; my code works with your sample input. If you have other input cases, then edit your question or ask a new one.

– Tim Biegeleisen
Mar 28 at 15:54






@FILO_q Not sure what more I can say here; my code works with your sample input. If you have other input cases, then edit your question or ask a new one.

– Tim Biegeleisen
Mar 28 at 15:54














ok so summarized: i want to get the itId and then afterwards the unknown number of OK: codes.

– FILO_q
Mar 28 at 15:59





ok so summarized: i want to get the itId and then afterwards the unknown number of OK: codes.

– FILO_q
Mar 28 at 15:59













@FILO_q I edited my question to also capture Id's using an alternation. Both Ids and codes get printed, so hopefully the output would be clear.

– Tim Biegeleisen
Mar 28 at 16:05






@FILO_q I edited my question to also capture Id's using an alternation. Both Ids and codes get printed, so hopefully the output would be clear.

– Tim Biegeleisen
Mar 28 at 16:05














that is very close, but it returns now "itId=<1232>" instead of only "1232". If you can tell me how to fix it im done!

– FILO_q
Mar 28 at 16:19






that is very close, but it returns now "itId=<1232>" instead of only "1232". If you can tell me how to fix it im done!

– FILO_q
Mar 28 at 16:19














0
















If you want to capture the digits in itId=<1232> followed by subsequent captures of what is after OK: in that order , you could make use of the G anchor to assert the position at the end of the previous match.



Match the itId digits in the first capturing group and the value of OK: in the second capturing group:



itId=<(d+)> Code < |G(?!^)[OK:([A-Z0-9]+)]s*


In Java:



String ok_pattern = "itId=<(\d+)> Code < |\G(?!^)\[OK:([A-Z0-9]+)\]\s*";


Explanation




  • itId=<(d+)> Code < Match the first part and capture 1+ digits in group 1


  • | Or


  • G(?!^) End of the previous match, not at the start


  • [OK:([A-Z0-9]+)]s* Match [OK:, then capture your value in group 2 and match ] followed by 0+ whitespace chars

Regex demo | Java demo



Note that if you want to match more than ([A-Z0-9]+) you could also use a negated character class to match not a square bracket ([^]]+)



For example, you might check for the existence of the groups:



String ok_pattern = "itId=<(\d+)> Code < |\G(?!^)\[OK:([^]]+)\]\s*";
Pattern p_ok = Pattern.compile(ok_pattern);
String testString = "RANDOMTEXT itId=<1232> Code < [OK:AZ1000105] [OK:10000006] [OK:F1000000007] > RANDOMTEXT";
Matcher m = p_ok.matcher(testString);

while(m.find())
if (null != m.group(1))
System.out.println("itId: " + m.group(1));

if (null != m.group(2))
System.out.println("Ok code: " + m.group(2));







share|improve this answer



























  • how can i actually access the values then? if i try to use: ``` Matcher m = p_ok.matcher(testString); if(m.find()) System.out.println(m.group(5)); ``` I dont get any results.

    – FILO_q
    Mar 28 at 16:21












  • Did you check the demo link? For exmple ideone.com/QgveK8 You can check for the existence of the different groups.

    – The fourth bird
    Mar 28 at 16:22











  • my network actually blocks the java demo and i cant access it...

    – FILO_q
    Mar 28 at 16:25











  • @FILO_q I have added and example and perhaps test it here rextester.com/ZTEMBP76897

    – The fourth bird
    Mar 28 at 16:32






  • 1





    Thank you very much. You and Tim Biegeleisen solved it at the same time... thanks for your help!

    – FILO_q
    Mar 28 at 16:35















0
















If you want to capture the digits in itId=<1232> followed by subsequent captures of what is after OK: in that order , you could make use of the G anchor to assert the position at the end of the previous match.



Match the itId digits in the first capturing group and the value of OK: in the second capturing group:



itId=<(d+)> Code < |G(?!^)[OK:([A-Z0-9]+)]s*


In Java:



String ok_pattern = "itId=<(\d+)> Code < |\G(?!^)\[OK:([A-Z0-9]+)\]\s*";


Explanation




  • itId=<(d+)> Code < Match the first part and capture 1+ digits in group 1


  • | Or


  • G(?!^) End of the previous match, not at the start


  • [OK:([A-Z0-9]+)]s* Match [OK:, then capture your value in group 2 and match ] followed by 0+ whitespace chars

Regex demo | Java demo



Note that if you want to match more than ([A-Z0-9]+) you could also use a negated character class to match not a square bracket ([^]]+)



For example, you might check for the existence of the groups:



String ok_pattern = "itId=<(\d+)> Code < |\G(?!^)\[OK:([^]]+)\]\s*";
Pattern p_ok = Pattern.compile(ok_pattern);
String testString = "RANDOMTEXT itId=<1232> Code < [OK:AZ1000105] [OK:10000006] [OK:F1000000007] > RANDOMTEXT";
Matcher m = p_ok.matcher(testString);

while(m.find())
if (null != m.group(1))
System.out.println("itId: " + m.group(1));

if (null != m.group(2))
System.out.println("Ok code: " + m.group(2));







share|improve this answer



























  • how can i actually access the values then? if i try to use: ``` Matcher m = p_ok.matcher(testString); if(m.find()) System.out.println(m.group(5)); ``` I dont get any results.

    – FILO_q
    Mar 28 at 16:21












  • Did you check the demo link? For exmple ideone.com/QgveK8 You can check for the existence of the different groups.

    – The fourth bird
    Mar 28 at 16:22











  • my network actually blocks the java demo and i cant access it...

    – FILO_q
    Mar 28 at 16:25











  • @FILO_q I have added and example and perhaps test it here rextester.com/ZTEMBP76897

    – The fourth bird
    Mar 28 at 16:32






  • 1





    Thank you very much. You and Tim Biegeleisen solved it at the same time... thanks for your help!

    – FILO_q
    Mar 28 at 16:35













0














0










0









If you want to capture the digits in itId=<1232> followed by subsequent captures of what is after OK: in that order , you could make use of the G anchor to assert the position at the end of the previous match.



Match the itId digits in the first capturing group and the value of OK: in the second capturing group:



itId=<(d+)> Code < |G(?!^)[OK:([A-Z0-9]+)]s*


In Java:



String ok_pattern = "itId=<(\d+)> Code < |\G(?!^)\[OK:([A-Z0-9]+)\]\s*";


Explanation




  • itId=<(d+)> Code < Match the first part and capture 1+ digits in group 1


  • | Or


  • G(?!^) End of the previous match, not at the start


  • [OK:([A-Z0-9]+)]s* Match [OK:, then capture your value in group 2 and match ] followed by 0+ whitespace chars

Regex demo | Java demo



Note that if you want to match more than ([A-Z0-9]+) you could also use a negated character class to match not a square bracket ([^]]+)



For example, you might check for the existence of the groups:



String ok_pattern = "itId=<(\d+)> Code < |\G(?!^)\[OK:([^]]+)\]\s*";
Pattern p_ok = Pattern.compile(ok_pattern);
String testString = "RANDOMTEXT itId=<1232> Code < [OK:AZ1000105] [OK:10000006] [OK:F1000000007] > RANDOMTEXT";
Matcher m = p_ok.matcher(testString);

while(m.find())
if (null != m.group(1))
System.out.println("itId: " + m.group(1));

if (null != m.group(2))
System.out.println("Ok code: " + m.group(2));







share|improve this answer















If you want to capture the digits in itId=<1232> followed by subsequent captures of what is after OK: in that order , you could make use of the G anchor to assert the position at the end of the previous match.



Match the itId digits in the first capturing group and the value of OK: in the second capturing group:



itId=<(d+)> Code < |G(?!^)[OK:([A-Z0-9]+)]s*


In Java:



String ok_pattern = "itId=<(\d+)> Code < |\G(?!^)\[OK:([A-Z0-9]+)\]\s*";


Explanation




  • itId=<(d+)> Code < Match the first part and capture 1+ digits in group 1


  • | Or


  • G(?!^) End of the previous match, not at the start


  • [OK:([A-Z0-9]+)]s* Match [OK:, then capture your value in group 2 and match ] followed by 0+ whitespace chars

Regex demo | Java demo



Note that if you want to match more than ([A-Z0-9]+) you could also use a negated character class to match not a square bracket ([^]]+)



For example, you might check for the existence of the groups:



String ok_pattern = "itId=<(\d+)> Code < |\G(?!^)\[OK:([^]]+)\]\s*";
Pattern p_ok = Pattern.compile(ok_pattern);
String testString = "RANDOMTEXT itId=<1232> Code < [OK:AZ1000105] [OK:10000006] [OK:F1000000007] > RANDOMTEXT";
Matcher m = p_ok.matcher(testString);

while(m.find())
if (null != m.group(1))
System.out.println("itId: " + m.group(1));

if (null != m.group(2))
System.out.println("Ok code: " + m.group(2));








share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 28 at 19:48

























answered Mar 28 at 15:51









The fourth birdThe fourth bird

41.3k9 gold badges20 silver badges37 bronze badges




41.3k9 gold badges20 silver badges37 bronze badges















  • how can i actually access the values then? if i try to use: ``` Matcher m = p_ok.matcher(testString); if(m.find()) System.out.println(m.group(5)); ``` I dont get any results.

    – FILO_q
    Mar 28 at 16:21












  • Did you check the demo link? For exmple ideone.com/QgveK8 You can check for the existence of the different groups.

    – The fourth bird
    Mar 28 at 16:22











  • my network actually blocks the java demo and i cant access it...

    – FILO_q
    Mar 28 at 16:25











  • @FILO_q I have added and example and perhaps test it here rextester.com/ZTEMBP76897

    – The fourth bird
    Mar 28 at 16:32






  • 1





    Thank you very much. You and Tim Biegeleisen solved it at the same time... thanks for your help!

    – FILO_q
    Mar 28 at 16:35

















  • how can i actually access the values then? if i try to use: ``` Matcher m = p_ok.matcher(testString); if(m.find()) System.out.println(m.group(5)); ``` I dont get any results.

    – FILO_q
    Mar 28 at 16:21












  • Did you check the demo link? For exmple ideone.com/QgveK8 You can check for the existence of the different groups.

    – The fourth bird
    Mar 28 at 16:22











  • my network actually blocks the java demo and i cant access it...

    – FILO_q
    Mar 28 at 16:25











  • @FILO_q I have added and example and perhaps test it here rextester.com/ZTEMBP76897

    – The fourth bird
    Mar 28 at 16:32






  • 1





    Thank you very much. You and Tim Biegeleisen solved it at the same time... thanks for your help!

    – FILO_q
    Mar 28 at 16:35
















how can i actually access the values then? if i try to use: ``` Matcher m = p_ok.matcher(testString); if(m.find()) System.out.println(m.group(5)); ``` I dont get any results.

– FILO_q
Mar 28 at 16:21






how can i actually access the values then? if i try to use: ``` Matcher m = p_ok.matcher(testString); if(m.find()) System.out.println(m.group(5)); ``` I dont get any results.

– FILO_q
Mar 28 at 16:21














Did you check the demo link? For exmple ideone.com/QgveK8 You can check for the existence of the different groups.

– The fourth bird
Mar 28 at 16:22





Did you check the demo link? For exmple ideone.com/QgveK8 You can check for the existence of the different groups.

– The fourth bird
Mar 28 at 16:22













my network actually blocks the java demo and i cant access it...

– FILO_q
Mar 28 at 16:25





my network actually blocks the java demo and i cant access it...

– FILO_q
Mar 28 at 16:25













@FILO_q I have added and example and perhaps test it here rextester.com/ZTEMBP76897

– The fourth bird
Mar 28 at 16:32





@FILO_q I have added and example and perhaps test it here rextester.com/ZTEMBP76897

– The fourth bird
Mar 28 at 16:32




1




1





Thank you very much. You and Tim Biegeleisen solved it at the same time... thanks for your help!

– FILO_q
Mar 28 at 16:35





Thank you very much. You and Tim Biegeleisen solved it at the same time... thanks for your help!

– FILO_q
Mar 28 at 16:35


















draft saved

draft discarded















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55401617%2frepeating-regular-expression%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현