Regular Expression to match two separate phrases

13,805

Solution 1

If you can be sure that they will appear in that order, if at all, then this should work:

(<query 1>).*(<query 2>)

E.g.

(Average Latency \(last \d+ queries\)).*(Current QPS \(last \d+s, ignored \d+\))

You may need to check that the . operator matches newlines in your tool.

Solution 2

my first suggest is to simply add the two patterns in your regular expression in any order you expect them to appear

/($regex1.*?$regex2|$regex2.*?$regex1)/
Share:
13,805
Madean
Author by

Madean

Updated on July 14, 2022

Comments

  • Madean
    Madean almost 2 years

    I am looking for a regular expression that can ensure two phrases showing up on a webpage at the same time.

    The two phrases I need to ensure on the web are Current QPS (last 10s, ignored 0) and Average Latency (last 100 queries)

    The webpage looks like (The query time would be different, but text won't change):

    Query Statistics
    
    Average QPS 25.3673   
    Average Latency 0.1002   
    Average Latency (last 100 queries) 0.0834   # Match this one, ignore output-0,0834
    Average Search Latency 0.0555   
    Average Docsum Latency 0.0330   
    Sampling period 3133524.9570   
    Current QPS (last 10s, ignored 0) 24.8000  # Also match this one, ignore output 24.8000 
    Peak QPS 170.9000   
    Number of requests 79717858   
    Number of queries 79489080 
    

    I am able to match each phrase on the website, but not the two phrases together. How can I make my tool ignore the content between the two phrases?

    P.S. I am not programming in any language here, the regex will be put into a tool that accepts regex.

  • Madean
    Madean almost 12 years
    But I don't need the text between the two phrases. How can you get rid of them?
  • vergenzt
    vergenzt almost 12 years
    What tool are you using, and what are you trying to do if/when those patterns match?
  • Madean
    Madean almost 12 years
    I am using an enterprise tool. Basically the tool accepts the regex and return the page status as good if the two strings are found.
  • vergenzt
    vergenzt almost 12 years
    When you say you don't need the text between the two phrases, and you want to "ignore" it, do you mean that you want the text between the two patterns to not matter in whether or not the patterns match? If all you're doing is checking for matches, then the in-between text will not affect the results.
  • Madean
    Madean almost 12 years
    I am using an enterprise tool. Basically the tool accepts the regex and return the page status as good if the two strings are found. I think my tool accepts . to match newlines, since it is able to return all the contents between the two phrases.
  • Madean
    Madean almost 12 years
    Yes, the text between doesn't matter. Actually, they will always stay same except the outputs. Your suggestion makes sense. I am just curious about how to parse out the two phrases from that page without redundant matches. :)
  • vergenzt
    vergenzt almost 12 years
    Usually when regex is used in a programming context, there's a way to extract matched groups (matching text within parentheses--what you're looking for) and discard the rest. Is that what you mean?
  • Madean
    Madean almost 12 years
    Thanks for the help, but unfortunately the expression doesn't work out in my tool. One quesiont - does the .*?do the work of ignoring the middle part?
  • Hachi
    Hachi almost 12 years
    .*? matches any (the smallest) part between the two expressions ; maybe you have to set a flag for . to match newlines