-2

I have this data:

Turning on API signature validation for service : PGL
Turning off API signature validation for service : PLATFORM
Turning off API signature validation for service : WEBWORKS
Turning on API signature validation for service : TIMER_SINK
Turning on API signature validation for service : MESSAGING
Turning on API signature validation for service : EMAIL
Turning off API signature validation for service : USER_SECURITY
Initializing MDM  for :USER_SECURITY
<MDM/>

I want to catch a string after service : I managed to do it by: service.:.\K\w+

BUT: Earlier I tried to do it with diffrent way which is service:\s* ([^\n\r]*) but it returns me my value with service. I got this way from this article Regex to get the words after matching string and no bigger effects.

Could anyone explain me this pattern ([\n\r].*Object Name:\s*([^\n\r]*))

Community
  • 1
  • 1
Michał M
  • 618
  • 5
  • 13

3 Answers3

2
[\n\r].*Object Name:\s*([^\n\r]*)

[\n\r] -> match end of a line

.* -> match any character N times

Object Name: -> match "Object Name:"

\s* -> match N whitespaces

[^\n\r]* -> any character except for a new line, N times

Parentheses like () define matching groups, so in many API you will use something like that:

matchresult.at(0) = the whole match

matchresult.at(1) = it's what you want, i.e characters that match [^\n\r]*

So all the characters between the last white space (the last white space after "Object Name:") and the end of the line.

baddger964
  • 1,199
  • 9
  • 18
1
[\n\r].*Object Name:\s*([^\n\r]*)

Let's break it down:

[\n\r] this matches new line. Both \n and \r could match new line in different cases. Sometimes the new line will include both of them: \r\n. [\n\r] matches only one of these, since there is no quantifier after the square brackets.

. - matches everything except new line. The * means it will match zero to unlimited times. By default, the * is greedy - i.e., it will match as many times as possible.

Object Name: - matches literally Object Name: - including the :.

\s matches every whitespace character - including space, tab, new line, etc. The * - same is before - zero to unlimited times, greedy.

([^\n\r]*) - since this is in parenthesis, this will be "captured", i.e. this is the first and only capture group in this regex. This means that if you want to use that later in a replace, you can access it with $1.

[] - the square brackets are used for a list of allowed characters. In this case, since we have the special character ^ inside, this would be a list of disallowed characters. So [^\n\r]* would mean that it would match everything except new line - zero to unlimited times. The difference with the .* expression lies only in the case of multiline searching enabled, where the . can also match new line.

Maria Ivanova
  • 1,146
  • 10
  • 19
0

Updated: Your can use the following regex :

(?<=:\s).*

Demo and Explaination

Shekhar Khairnar
  • 2,643
  • 3
  • 26
  • 44