Jump to content

how to use (?s) and (?m)


Recommended Posts

Hi All,

I'm trying to match part of text in one big string including many lines. For example, my string is:

Shipping Address:

xxxxxx

xxxxxx

xxxxxxxxx

xxxxxxxxxxx

Shipping Date:

I tried to get the text between Shipping Address: and Shipping Date:.

I use (?:Shipping Address:)\r\n((?s)*)(?:Shipping Method)

It can't return the that multiple lines of "x". Also don't know (?m) can be used for some help?

Thanks guys.

Lou

Link to comment
Share on other sites

(?s)Shipping Address:\r\n(.*?)\r\nShipping Date:

?s is an option, put it at the beginning of the pattern. Also some unneeded substrings.

\1 (first capturing group) returns the text between both, with no CRLFs before or after

Regards,Josh

Link to comment
Share on other sites

(?s)Shipping Address:\r\n(.*?)\r\nShipping Date:

?s is an option, put it at the beginning of the pattern. Also some unneeded substrings.

\1 (first capturing group) returns the text between both, with no CRLFs before or after

Thank you very much for your quick response. It works for me. But why you use (.*?) not (.*)? some difference? it works same for me.

Lou

Link to comment
Share on other sites

Thank you very much for your quick response. It works for me. But why you use (.*?) not (.*)? some difference? it works same for me.

Lou

I think there is a difference between (.*?) and (.*?)

(.*?) searches for 1 group of a character string that is unlimited, but it finds the smallest possible match (the "?" inverts the greediness of the "*")

(.*)? however searches for 1 group of an unlimited number of characters that may or may not exist (the "?" is now a separate quantifier")

I think?

Edited by Paulie
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...