0

I am trying to create a regexp with replace for use in a java embedded application that will pass a URL through an on click. What I have so far is

(bi.)(.*\.)([^.]+)(.com)$ with replace set to $2$3$4

This, however, does not allow for passing to environments without the second variable. What I need is something that will say IF .*\. is prd001 THEN '' ELSE .*\. I need to negate the prd001 where it exists, but pass that portion through if it is anything else in the substitution portion.

I am very new to these concepts and if you ask what language this is for, I probably could not tell you other than our application is written in JAVA. I do know I will need escape characters later, and am confident in that portion. Just lost with the regex. Thank you for any help you can provide

2 Answers 2

1

Assuming I understand your request and your original regex is correct (I thought you might have meant bi\. in the initial capture group), the following should work:

(bi.)(?:(?:prd001\.)|(.*\.))([^.]+)(.com)

as shown in http://rubular.com/r/oU1ls2Gy6x and http://rubular.com/r/YDhYObKe1c

The ?: construct tells the regex machinery to match the following expression but not create a capture group. So the second capture group will be empty if any only if prod001. is not present there.

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

9 Comments

Your rubular link only has one example url. Add an example for a non prod url, and maybe other edge cases
@Bohemian Do you know how to do that (include multiple test strings) within a single Rubular?
@Bohemian I added a non-prod test string, but given the ambiguities in the question, I'm not sure what edge cases to include.
Thank you for the answer. I am testing this in notepad++ but it is not exluding 'prd001.' The examples I am working with are: What is input > What I need bi.bidev-30000.123.com > bidev-30000.123.com, bi.prd001.123.com > 123.com, bi.test30000.123.com > test30000.123.com
Was it just the prd001 example that didn't work? They all work for me in Sublime 2. Is Notepad a reference to Windows' Notepad?
|
0

Try to change the regex to (bi.)(.*\.)?([^.]+)(.com)$

Now it will only use the (.*\.) if it is present, and thus if not present the $2 will be empty.

1 Comment

I did end up incorporating the question mark in order to be sure to gather examples such as bi.123.com. Thanks for this, it was a great help, but alone it was not the answer.

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.