| |

VerySource

 Forgot password?
 Register
Search
View: 1246|Reply: 10

String wild house matching algorithm

[Copy link]

2

Threads

8

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 Invalid IP Address

Post time: 2020-1-20 18:40:01
| Show all posts |Read mode
The program requires the following functions:
1. Prompt the user for two strings,
2. Read two strings entered by the user
3. Match based on the two strings read in, for example:
* i * tooer, dietooer indicates that the two strings match, and returns true.
co * de, acode indicates that the two strings do not match, and returns false.

How can this function be implemented in the Java language?
Reply

Use magic Report

2

Threads

8

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 Invalid IP Address

 Author| Post time: 2020-1-29 18:27:01
| Show all posts
In addition, * indicates that this place can have 0 or more characters.
Reply

Use magic Report

0

Threads

7

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 Invalid IP Address

Post time: 2020-1-30 21:09:01
| Show all posts
Somewhat difficult, (if both strings contain *)
I do n’t know, I picked the bird right?
If only one has a *, it seems like using regular expressions. I haven't watched Javascript for more than a year
If both have *, I think it may be divided into multiple strings for matching by *
Reply

Use magic Report

2

Threads

8

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 Invalid IP Address

 Author| Post time: 2020-1-31 05:45:02
| Show all posts
With regular expressions? Can you elaborate?
Reply

Use magic Report

0

Threads

12

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-31 11:45:01
| Show all posts
public static boolean isRegex (String s, String reg) // s is the original string, and reg is the matching string
{
return s.matches (reg.replaceAll ("\\*", ". *"));
}
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-31 19:36:01
| Show all posts
There is a deviation in the meaning upstairs. The user did not enter "*"
Reply

Use magic Report

2

Threads

8

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 Invalid IP Address

 Author| Post time: 2020-2-1 07:27:01
| Show all posts
Thank youchen96542, I tried the regular expression method and it was successful. But when there are two or more asterisks in the string, this method is wrong. How to solve this problem?
Reply

Use magic Report

0

Threads

12

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-2-1 12:45:01
| Show all posts
Send specific examples to see
Reply

Use magic Report

2

Threads

8

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 Invalid IP Address

 Author| Post time: 2020-2-6 16:15:01
| Show all posts
Just now I ran the program again and found that it works even when there are multiple "*" in the string, but the problem is that when "*" appears in the first place, the program runs wrong.
Reply

Use magic Report

3

Threads

17

Posts

14.00

Credits

Newbie

Rank: 1

Credits
14.00

 China

Post time: 2020-2-8 18:30:01
| Show all posts
import java.util.regex. *;

public class TwoWords
{
public static void main (String [] args)
{
if (args.length! = 2) System.out.println ("wrong number of words");
Pattern p = Pattern.compile (args [0]);
Matcher m = p.matcher (args [1]);
if (m.matches ()) System.out.println ("match");
}
}
run:
java TwoWords. * ab. * dd fdsfdsjab ** dd
print:
match
I don't know what the * sign in lz means. Is it an asterisk in regular expression or ordinary asterisk?
There is also running java TwoWords\\* ab. * Dd * abw ** dd will not print the match, what is the reason for this? I think it should match
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list