1

Here is my regex:

STATICSTRING\s[a-zA-Z]:\\[\\\S|*\S]?.*$|STATICSTRING\s\w*

as you can see there are two patterns, \s[a-zA-Z]:\\[\\\S|*\S]?.*$ and \s\w* which is combined with a | operator. and the STATICSTRING is repeated in each one.

Is there any way to write STATICSTRING once?

2
  • Use | and a grouping construct: STATICSTRING\s(?:[a-zA-Z]:\\[\\\S|*\S]?.*$|\w*). However, \\[\\\S|*\S]?.* looks too suspicious. What should it match? Commented Jun 25, 2018 at 7:03
  • @WiktorStribiżew please submit it as answer ;) Commented Jun 25, 2018 at 7:04

1 Answer 1

2

You may use an | alternation operator in a grouping construct to group two subpatterns:

STATICSTRING\s(?:[a-zA-Z]:\\[\\\S|*\S]?.*$|\w*)
              ^^^                         ^   ^

However, the \\[\\\S|*\S]?.* part looks like a user error. It matches a \, then 1 or 0 occurrences of \, |, * or any non-whitespace char, and then .* matches any 0+ chars up to the end of the line. Make sure you fix it if you intended to match anything else. But \w* branch will always "win" as it always matches (either an empty string or a letter (and [a-zA-Z] also matches a letter)). So, the pattern above is equal to STATICSTRING\s\w*.

Sign up to request clarification or add additional context in comments.

16 Comments

the first part is a pattern for file path :)
@Nofuzy It is not important, the pattern you have needs a fix.
I do not know much about regex, I just copy and paste them from net with my needs. could you give the fixed one?
[a-zA-Z]:\\[\\\S|*\S]?.*$ = [a-zA-Z]:\\\S.*$, and it matches a letter, :\, then any char other than whitespace and then any 0+ chars to the end of the line. No, it is not.
Well, try [a-zA-Z]:(?:\\[^\/]+)*\\.*?\.\S+ or even [a-zA-Z]:\\.*?\.\S+ but no one guarantees that it won't over-match in case there is a no-match and a match on a single line.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.