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

  • Home
  • SEARCH
  • 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 4065476
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:59:15+00:00 2026-05-20T15:59:15+00:00

I’m doing some regular expression gymnastics. I set myself the task of trying to

  • 0

I’m doing some regular expression gymnastics. I set myself the task of trying to search for C# code where there is a usage of the as-operator not followed by a null-check within a reasonable amount of space. Now I don’t want to parse the C# code. E.g. I want to capture code snippets such as

    var x1 = x as SimpleRes;
    var y1 = y as SimpleRes;
    if(x1.a == y1.a)

however, not capture

    var x1 = x as SimpleRes;
    var y1 = y as SimpleRes;
    if(x1 == null)

nor for that matter

    var x1 = x as SimpleRes;
    var y1 = y as SimpleRes;
    if(somethingunrelated == null) {...}
    if(x1.a == y1.a)

Thus any random null-check will count as a “good check” and hence not found.

The question is: How do I match something while ensuring something else is not found in its sourroundings.

I’ve tried the naive approach, looking for ‘as’ then doing a negative lookahead within a 150 characters.

\bas\b.{1,150}(?!\b==\s*null\b)

The above regular expression matches all of the above examples infortunately. My gut tells me, the problem is that the looking ahead and then doing negative lookahead can find many situations where the lookahead does not find the ‘== null’.

If I try negating the whole expression, then that doesn’t help either, at that would match most C# code around.

  • 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-20T15:59:15+00:00Added an answer on May 20, 2026 at 3:59 pm

    I love regex gymnastics! Here is a commented PHP regex:

    $re = '/# Find all AS, (but not preceding a XX == null).
        \bas\b               # Match "as"
        (?=                  # But only if...
          (?:                # there exist from 1-150
            [\S\s]           # chars, each of which
            (?!==\s*null)    # are NOT preceding "=NULL"
          ){1,150}?          # (and do this lazily)
          (?:                # We are done when either
            (?=              # we have reached
              ==\s*(?!null)  # a non NULL conditional
            )                #
          | $                # or the end of string.
          )
        )/ix'
    

    And here it is in Javascript style:

    re = /\bas\b(?=(?:[\S\s](?!==\s*null)){1,150}?(?:(?===\s*(?!null))|$))/ig;
    

    This one did make my head hurt a little…

    Here is the test data I am using:

    text = r"""    var x1 = x as SimpleRes;
        var y1 = y as SimpleRes;
        if(x1.a == y1.a)
    
    however, not capture
        var x1 = x as SimpleRes;
        var y1 = y as SimpleRes;
        if(x1 == null)
    
    nor for that matter
        var x1 = x as SimpleRes;
        var y1 = y as SimpleRes;
        if(somethingunrelated == null) {...}
        if(x1.a == y1.a)"""
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.