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

Question

Question

Regex - trying to remove a leading space

asked on January 10, 2024

Hello all,

In the below example I am trying to retrieve only 'gina' from the string. As you can see I am able to retrieve ' gina' with the leading space. Does anyone know how to remove that leading space. My compares aren't working because of the space.

 

 

Sincerely,

 

Lloyd

0 0

Answer

SELECTED ANSWER
replied on January 10, 2024

Try this [^\w+,](\w+)_\d

 

2 0
replied on January 10, 2024

WOW. Awesome. Would never have guessed that one.

Why would you use _\d instead of [^_\d]? I thought you would not want to match the digits.

Thank you...:)

0 0
replied on January 10, 2024 Show version history

Hi Lloyd. By putting the () around the \w+, I am specifying what the match group is, which means whatever is outside the match group is not returned. After the Match Group I just needed to specify what needs to be found to stop the match group from capturing as "_" and digits are included as word characters. I could have just used [^\w+,](\w+)_  which would mean to stop capturing the work characters once it saw an underscore instead on an underscore followed by a digit"_". I always recommend you try your patterns with multiple examples to ensure that all possible combinations are covered by the regex. FYI, this works as well \w+,\s*?(\w+)_

2 0

Replies

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

Sign in to reply to this post.