0

currently I have a table that saves the logs for each change in the endpoints, however I need to show on screen to the user which endpoint has been changed, and which Id's have been sent. I currently have these examples:

  • /admin/account/bot/6/layout/properties
  • /admin/bot/4/block/content/button/34/label
  • /admin/bot/4/block/content/text/18/message

it is possible to make a regular expression to classify each endpoint, for example:

/admin/account/bot/6/layout/properties
name: Update Bot properties
BotId: 6

/admin/bot/4/block/content/text/18/message
name: Update message in Text
BotId: 4
TextId: 18

and extract each value ?

thanks.

2 Answers 2

1

Depends on the exact structure of the strings you are trying to match; not everything is regular. A basic approach here would be:

private static final Pattern ACCOUNT_PROPERTIES = 
  Pattern.compile("/admin/account/bot/(\\d+)/layout/properties");
private static final Pattern BLOCK_CONTENT = 
  Pattern.compile("/admin/bot/(\\d+)/block/content/(\\w+)/(\\d+)/(\\w+)");

public void handle(String path) {
   Matcher m;

   m = ACCOUNT_PROPERTIES.matcher(path);
   if (path.matches()) {
       handlePropertiesUpdate(Integer.parseInt(m.group(1)));
       return;
   }

   m = BLOCK_CONTENT.matcher(path);
   if (path.matches()) {
       int botId = Integer.parseInt(m.group(1));
       String objType = m.group(2);
       int objId = Integer.parseInt(m.group(3));
       String updType = m.group(4);
       handleBlockContent(botId, objType, objId, updType);
   }

   throw new NotAcceptedException();
}

etcetera.

Trying to make one regexp to catch it all seems... ill-advised. It'd be one heck of a job to try to figure out which group goes where, and an update in one of these structures would then neccessity updating all your code. There are ways to name groups, which is a slight improvement, but you're still looking at a gigantic regexp. If the aim is to 'make it run faster by running less code', I have bad news. Regexps make no guarantees about performance and have O(n^2) worst case if you use certain regex features. If you want speed, steer away from them, or at least be an expert so you know what and how to make them run quickly.

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

Comments

0

I don't know of a foolproof way to generate the exact names you expect here, but as for the key-value pairs, a regex pattern matcher can handle this:

String input = "/admin/bot/4/block/content/button/34/label";
String pattern = "/(\\w+)/(\\d+)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);

while (m.find( )) {
    System.out.println(m.group(1) + "Id: " + m.group(2));
}

This prints:

botId: 4
buttonId: 34

Comments

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.