J
J
Justperfect2021-08-22 18:02:18
Java
Justperfect, 2021-08-22 18:02:18

What is the correct regex for pattern?

Friends, I ask for your help. I am writing a program that generates a to-do list through the console using commands.

I need to teach the program to respond differently to two input patterns:
First: "ADD + Text" (where "ADD" is the name of the command, "Text" is the text). For example: ADD Case 4
Second: "ADD + # + Text" (where "ADD" is the name of the team, "#" is the sequence number to be assigned to the case, "Text" is the text). For example: ADD 4 Thing 4

Separating these patterns I plan to implement this with Pattern - Matcher. But it is not possible to correctly set the condition for Patterna.

Now I use the following regular expressions:
For the first one: Pattern patternAdd = Pattern.compile("
For the second one: Pattern patternAddX = Pattern.compile("ADD + \\s[0-9] + [a-zA-Z0-9]");

Including tried their various variations, but to no avail ... Can you tell me how to write regex correctly? Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
berng, 2021-08-22
@berng

Try this:
Variant strings:
$a="ADD sfdsf"
or
$a="ADD 123 sfdsf"
Regular (perl):
if($a =~ /ADD\s+(\d)+[^\d].+ /)
or like this:
if($a =~ /ADD\s+[\d]+[^\d].+/)
check only one regular expression, it will return false on the first line and true on the second.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question