You are viewing limited content. For full access, please sign in.

Question

Question

(Pattern matching \s?) Is it correct for delete espace?

asked on January 31, 2018

Hi all,

 

I'm using Pattern Matching to get what I want but it looks like I make mistake.

 

This is my pattern : Field:\s?(.*)\s?Numb

For me, the "\s?" means 1 or 0 espace, right?

My first space didn't pick, but why my last one was picked?

 

0 0

Answer

SELECTED ANSWER
replied on January 31, 2018

Pattern Matching will always match as much as possible. So in this case, the first space is part of the pattern before the match group, while the last one is part of the match group itself because it matches the pattern for the group and you're saying it's ok to have no spaces before "Numb".

Something like Field:\s?(.*[^\s])\s?Numb specifically says the last character in the match group is not a space ("[^\s]"), so it will leave the last space out of the returned value.

1 0
replied on January 31, 2018

Miruna,

 

Last question about pattern

How can I do if the "caractere" is blank or a word?

For exemple : "Field: Ab 123 Number" or "Field: Ab 123Word Number"

 

Field:\s?(.*[^\s]|[^Word])\s?Numb is it alright?

 

 

0 0
replied on January 31, 2018

By "word" you mean a specific word that's always the same or a word that could change between values?

If it's the second case, how would you describe the value before it? Is it always 3 digits? Could it be letters?

0 0
replied on January 31, 2018

By "word" I mean a specific word (always the same).

0 0

Replies

replied on January 31, 2018

You can also add a '?' at the end of your match group.   
Field:\s?(.*?)\s?Number 

Adding the '?' tells the match to be non-greedy and will match the least amount of data while allowing the rest of the regexp to match.
Without the '?' it will match as much as it possibly can.

0 0
replied on January 31, 2018

Thanks all.

It's working.

The 2 ways are give me the same result.

 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.