Skip to content

How to find string between two strings #705

@hasanAjsf

Description

@hasanAjsf

I wrote the below code to find the use name in the text:

hi
user name: Rusty;

Which is in this case Rusty located betweenuser name and the ;

use regex::Regex;

fn main(){
    let re = Regex::new(r"(?<=(user name:))?P<name>[A-Za-z0-9\n\s\t]*(?=(;))").unwrap(); // (?<=Description)([^\n]*\n+)+(?=Issued by)
    let text = "hi\user name: Rusty;";

    let caps = re.captures(text).unwrap();

    println!("Use name: {}", &caps["name"]);
}

But I got this error:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Syntax(
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
regex parse error:
    (?<=(my name:))?P<name>[A-Za-z0-9\n\s\t]*(?=(;))
    ^^^^
error: look-around, including look-ahead and look-behind, is not supported
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
)', src\main.rs:4:78
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: process didn't exit successfully: `target\debug\server.exe` (exit code: 101)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions