Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6113197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:49:53+00:00 2026-05-23T14:49:53+00:00

I have written 2 REs to match several string sequences in a String. for

  • 0

I have written 2 REs to match several string sequences in a String. for e.g. lets assume the two regular expressions are RE1, RE2. The strings can be in these 4 forms;

1) Match ONLY RE1 'one or more times'
2) Match ONLY RE2 'one or more times'
3) Match RE1 'one or more times' AND match RE2 'one or more times'
4) Match NEITHER RE1 NOR RE2 

currently I am using if to check each of these, but I know its very expensive as I am doing the matching for a particular string several times. I thought of using ‘or’ | but the problem with that is regex will stop matching once it finds the first matching sequence and not continue to find others. I want to find matching sequences ‘one or more times’.

Update:

eg: RE1 = (\d{1,3}[a-zA-Z]?/\d{1,3}[a-zA-Z]?)
    RE2 = (\babc\b)
String: *some string* 100/64h *some string* 120h/90 *some string* abc 200/100 abc *some string* 100h/100f

Matches: '100/64h', '120h/90', 'abc', '200/100', 'abc', '100h/100f'

How can I merge these 2 REs to make my program efficient. I am using python to code this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:49:53+00:00Added an answer on May 23, 2026 at 2:49 pm

    You say “I know its very expensive as I am doing the matching for a particular string several times.” That suggests to me that you are running each RE several times. In that case, you are making a mistake that can be resolved without writing a more complex RE.

    re1_matches = re.findall(re1, text)
    re2_matches = re.findall(re2, text)
    

    This will result in two lists of matches. You can then perform boolean operations on those lists to generate whatever results you need; or you can concatenate them if you need all the matches in one list. You could also use re.match (match anchored at beginning of string) or re.search (match anywhere in the string) for each of these if you don’t need lists of results, but only need to know that there’s a match.

    In any case, creating a more complex RE in this case is probably not necessary or desirable.

    But it’s not immediately clear to me exactly what you want, so I could be wrong about that.


    Some suggestions about how to use boolean operators to process lists. First some setup:

    >>> re1 = r'(\d{1,3}[a-zA-Z]?/\d{1,3}[a-zA-Z]?)'
    >>> re2 = r'(\babc\b)'
    >>> re.findall(re1, text)
    ['100/64h', '120h/90', '200/100', '100h/100f']
    >>> re.findall(re2, text)
    ['abc', 'abc']
    >>> re1_matches = re.findall(re1, text)
    >>> re2_matches = re.findall(re2, text)
    >>> rex_nomatch = re.findall('conglomeration_of_sandwiches', text)
    

    and returns the first False result or the final result if all results are True.

    >>> not re1_matches and re2_matches
    False
    

    So if you want the list and not a flat boolean, you have to test the result you want last:

    >>> not rex_nomatch and re1_matches
    ['100/64h', '120h/90', '200/100', '100h/100f']
    

    Similarly:

    >>> not rex_nomatch and re2_matches
    ['abc', 'abc']
    

    If you just want to know that both REs generated matches, but don’t need any more, you can do this:

    >>> re1_matches and re2_matches
    ['abc', 'abc']
    

    Finally, here’s a compact way to get the concatenation if both REs generate matches:

    >>> re1_matches and re2_matches and re1_matches + re2_matches
    ['100/64h', '120h/90', '200/100', '100h/100f', 'abc', 'abc']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few regular expressions which are run against very long strings. However,
I have written this code: string _response = null; string _auth = Basic; Uri
I have written an AIR Application that downloads videos and documents from a server.
I have written some code in my VB.NET application to send an HTML e-mail
I have written a site in Prototype but want to switch to jQuery. Any
I have written a ruby script which opens up dlink admin page in firefox
I have written an AppleScript which when supplied with a Windows network link, will
I have written a DLL that uses MS Word to spell check the content
I have written an assembly I don't want other people to be able to
I have written something that uses the following includes: #include <math.h> #include <time.h> #include

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.