Jump to content
Sign in to follow this  
madmatrix

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

Recommended Posts

madmatrix

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

Share this post


Link to post
Share on other sites
JFee

(?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

Share this post


Link to post
Share on other sites
madmatrix

(?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

Share this post


Link to post
Share on other sites
JFee

Habit. There shouldn't be a difference, but I think it looks cleaner.


Regards,Josh

Share this post


Link to post
Share on other sites
madmatrix

Habit. There shouldn't be a difference, but I think it looks cleaner.

Thank you John!!!

Share this post


Link to post
Share on other sites
Paulie

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

Share this post


Link to post
Share on other sites
JFee

*Cough* Josh muttley

And yeah, that was what I meant to say :) And that is also why it is habit

Edited by JFee

Regards,Josh

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.