Home > OS >  Parsing string use regex
Parsing string use regex

Time:03-31

I have some string

022/03/17 05:53:40.376949    1245680 029 DSA- DREP COLS log debug S 1

Need get 1245680 number use regex statement I use next regular \d but many result in output.

CodePudding user response:

First: are you sure that you want to have regex? Wouldn't a string cut operation be better?

First for a fixed amount of 29 characters as this is the prefix length and then search for the next space in the rest of the string to clear the remainder.

If you have to use regex for some other reason (e.g. you don't have the ability to implement a routine where you need it), you can use a regex with a group to extract just the number you want: ^.{29}(\d ).*$

Here you have to use group(1) or any other reference to a group in the language you are using to get the value you want.

As the rest of the line also can contain numbers (and I suppose a variable amount of characters, if this a log entry), my simple attempts to use lookbehind and lookahead combination failed as they also found that other numbers in the line.

CodePudding user response:

If 022/03/17 05:53:40.376949 is always in that format, you can use:

\d{2}:\d{2}:\d{2}.\d{1,6}\s*(\d*)\s*

or more generally:

\d*\/\d*\/\d*\s .*?\s (\d*)

These will match the date/time segment, whitespace, the sequence of (captured) digits you desire, and then more whitespace.

  • Related