Regex Match all characters between two strings

后端 未结 14 1019
星月不相逢
星月不相逢 2020-11-21 07:42

Example: \"This is just\\na simple sentence\".

I want to match every character between \"This is\" and \"sentence\". Line breaks should be ignored. I can\'t figure o

相关标签:
14条回答
  • 2020-11-21 08:00

    Here is how I did it:
    This was easier for me than trying to figure out the specific regex necessary.

    int indexPictureData = result.IndexOf("-PictureData:");
    int indexIdentity = result.IndexOf("-Identity:");
    string returnValue = result.Remove(indexPictureData + 13);
    returnValue = returnValue + " [bytecoderemoved] " + result.Remove(0, indexIdentity); ` 
    
    0 讨论(0)
  • 2020-11-21 08:00

    There is a way to deal with repeated instances of this split in a block of text? FOr instance: "This is just\na simple sentence. Here is some additional stuff. This is just\na simple sentence. And here is some more stuff. This is just\na simple sentence. ". to matches each instance instead of the entire string, use below code:

    data = "This is just\na simple sentence. Here is some additional stuff. This is just\na simple sentence. And here is some more stuff. This is just\na simple sentence."
    
    pattern = re.compile('This is (?s).*? sentence')
    
    for match_instance in re.finditer(pattern, data):
        do_something(match_instance.group())
    
    0 讨论(0)
  • 2020-11-21 08:01

    For example

    (?<=This is)(.*)(?=sentence)
    

    Regexr

    I used lookbehind (?<=) and look ahead (?=) so that "This is" and "sentence" is not included in the match, but this is up to your use case, you can also simply write This is(.*)sentence.

    The important thing here is that you activate the "dotall" mode of your regex engine, so that the . is matching the newline. But how you do this depends on your regex engine.

    The next thing is if you use .* or .*?. The first one is greedy and will match till the last "sentence" in your string, the second one is lazy and will match till the next "sentence" in your string.

    Update

    Regexr

    This is(?s)(.*)sentence
    

    Where the (?s) turns on the dotall modifier, making the . matching the newline characters.

    Update 2:

    (?<=is \()(.*?)(?=\s*\))
    

    is matching your example "This is (a simple) sentence". See here on Regexr

    0 讨论(0)
  • 2020-11-21 08:05

    This worked for me (I'm using VS Code):

    for: This is just\na simple sentence

    Use: This .+ sentence

    0 讨论(0)
  • 2020-11-21 08:07

    I landed here on my search for regex to convert this print syntax between print "string", in Python2 in old scripts with: print("string"), for Python3. Works well, otherwise use 2to3.py for additional conversions. Here is my solution for others:

    Try it out on Regexr.com (doesn't work in NP++ for some reason):

    find:     (?<=print)( ')(.*)(')
    replace: ('$2')
    

    for variables:

    (?<=print)( )(.*)(\n)
    ('$2')\n
    

    for label and variable:

    (?<=print)( ')(.*)(',)(.*)(\n)
    ('$2',$4)\n
    

    How to replace all print "string" in Python2 with print("string") for Python3?

    0 讨论(0)
  • 2020-11-21 08:09

    Try This is[\s\S]*sentence, works in javascript

    0 讨论(0)
提交回复
热议问题