Why can't I capture more than one digit in substring?

Ankit Goyal

I am creating regex to extract various fields from logs files. I have created one regex using some tools and its almost complete. the only thing is for one field its extracting only one digit instead of full number. for better understanding I have saved it to below link.

My Regex Demo

Pattern:

/(?=[^P]*(?:ParNew|P.*ParNew|PSYoungGen|DefNew))^(?:).*(?P<ParNew_before_1>\d)K\->(?P<ParNew_after_1>\d+)K\((?P<young_heap_size>\d+)K\), (?P<par_new_duration>\d+\.\d+) secs\] (?P<ParNew_before_2>\d+)K\->(?P<ParNew_after_2>\d+)K\((?P<total_heap_size>\d+)/

String:

146372.273: [GC146372.274: [ParNew: 378633K->88155K(419456K), 0.0313803 secs] 9893391K->9602913K(12478080K), 0.0320299 secs] [Times: user=0.32 sys=0.01, real=0.03 secs]

Current Output:

Full match      `146372.273: [GC146372.274: [ParNew: 378633K->88155K(419456K), 0.0313803 secs] 9893391K->9602913K(12478080`
Group `ParNew_before_1`     `3`
Group `ParNew_after_1`      `88155`
Group `young_heap_size`     `419456`
Group `par_new_duration`    `0.0313803`
Group `ParNew_before_2`     `9893391`
Group `ParNew_after_2`      `9602913`
Group `total_heap_size`     `12478080`

Expected Output:

Full match      `146372.273: [GC146372.274: [ParNew: 378633K->88155K(419456K), 0.0313803 secs] 9893391K->9602913K(12478080`

Group ParNew_before_1 378633

Group `ParNew_after_1`      `88155`
Group `young_heap_size`     `419456`
Group `par_new_duration`    `0.0313803`
Group `ParNew_before_2`     `9893391`
Group `ParNew_after_2`      `9602913`
Group `total_heap_size`     `12478080`

In above example: Group ParNew_before_1 extracting only one digit.

Wiktor Stribiżew

There are three things I'd like to note here:

  • The lookahead should be placed after ^ (it will make more sense to check its pattern at the start of the string only)
  • The \d won't match more than 1 digit, add + after it to match 1 or more
  • .* is too greedy, use lazy .*?.

Use

^(?=[^P]*(?:ParNew|P.*ParNew|PSYoungGen|DefNew)).*?(?P<ParNew_before_1>\d+)K->(?P<ParNew_after_1>\d+)K\((?P<young_heap_size>\d+)K\), (?P<par_new_duration>\d+\.\d+) secs\] (?P<ParNew_before_2>\d+)K\->(?P<ParNew_after_2>\d+)K\((?P<total_heap_size>\d+)
 ^^^                                           ^  ^                      ^

See this regex demo

Also, you do not need to escape - that are not inside character classes.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why can't I catch the exception more than one time?

From Dev

Why can't I print more than one string?

From Dev

Why can't I install more than one application at once?

From Dev

Why can't I catch the exception more than one time?

From Dev

Why I can't reference to WITH expression more than one time?

From Dev

Why do I can't use more than one Java increment or decrement operators?

From Dev

Why can't I add a single Ellipse (patch) to more than one axis?

From Dev

Why can't I create a div that has more than one style element?

From Dev

Why we can't extends more than one class

From Dev

Why can't I query more than 1000 results for JIRA?

From Dev

Can't catch more than one interrupt

From Dev

jQuery.post: I can't post more than one time unless I refresh the page

From Dev

Why can't sed match more than one character at a time in this file?

From Dev

Why can't I use substring with a slash?

From Dev

Labview: I can't read the voltage from more than one channel (DAQmx read)

From Dev

Django : I can't add more than one row in a Formset without getting an error

From Dev

More than one instance of overloaded function matches the argument list and I can't find where the error happens

From Dev

Django : I can't add more than one row in a Formset without getting an error

From Dev

Why can't I Seq.take more than 5 values?

From Dev

Why can't I set the default factory in Typhoon more than once?

From Dev

When creating an instance of XmlDocument, why can't I use the SelectSingleNode("String").InnerText more than once?

From Dev

Why can't I create a group of circles with a dataset more than once in d3?

From Dev

Why more than one select input doesn't work? - Selectize

From Java

Why can't I name a sigilless variable v+digit?

From Dev

I wrote a simple particle filter and it won't work with more than a few hundred particles. I can't tell why

From Dev

Why does having more than one capture group in my regex crash my app?

From Dev

View can't be shared by more than one Listview

From Dev

can't run a couchbase cluster with more than one node

From Dev

Can't show more than one map annotation in WatchOS 2.0

Related Related

  1. 1

    Why can't I catch the exception more than one time?

  2. 2

    Why can't I print more than one string?

  3. 3

    Why can't I install more than one application at once?

  4. 4

    Why can't I catch the exception more than one time?

  5. 5

    Why I can't reference to WITH expression more than one time?

  6. 6

    Why do I can't use more than one Java increment or decrement operators?

  7. 7

    Why can't I add a single Ellipse (patch) to more than one axis?

  8. 8

    Why can't I create a div that has more than one style element?

  9. 9

    Why we can't extends more than one class

  10. 10

    Why can't I query more than 1000 results for JIRA?

  11. 11

    Can't catch more than one interrupt

  12. 12

    jQuery.post: I can't post more than one time unless I refresh the page

  13. 13

    Why can't sed match more than one character at a time in this file?

  14. 14

    Why can't I use substring with a slash?

  15. 15

    Labview: I can't read the voltage from more than one channel (DAQmx read)

  16. 16

    Django : I can't add more than one row in a Formset without getting an error

  17. 17

    More than one instance of overloaded function matches the argument list and I can't find where the error happens

  18. 18

    Django : I can't add more than one row in a Formset without getting an error

  19. 19

    Why can't I Seq.take more than 5 values?

  20. 20

    Why can't I set the default factory in Typhoon more than once?

  21. 21

    When creating an instance of XmlDocument, why can't I use the SelectSingleNode("String").InnerText more than once?

  22. 22

    Why can't I create a group of circles with a dataset more than once in d3?

  23. 23

    Why more than one select input doesn't work? - Selectize

  24. 24

    Why can't I name a sigilless variable v+digit?

  25. 25

    I wrote a simple particle filter and it won't work with more than a few hundred particles. I can't tell why

  26. 26

    Why does having more than one capture group in my regex crash my app?

  27. 27

    View can't be shared by more than one Listview

  28. 28

    can't run a couchbase cluster with more than one node

  29. 29

    Can't show more than one map annotation in WatchOS 2.0

HotTag

Archive