简体   繁体   中英

RegEx for matching spaces between a variable and commas

I am trying to parse through a url string so I can validate it for testing. My string looks

p_videoid=119110,p_videoepnumber= 0,p_videoairdate=NOT NULL,videoseason=null

My problem is for some videos there is a space in p_videoepnumber= 0. I need to find the substring that starts with p_videoepnumber and ends with the comma, and then remove all spaces.

I want my final output to look like:

p_videoid=119110,p_videoepnumber=0,p_videoairdate=NOTNULL,videoseason=null

I can't just remove all spaces from the string because there are certain values that have a space.

Based on what you said, you can simply do:

string.replace("= ","=");

If you only want the p_videoepnumber key to be changed:

string.replace("p_videoepnumber= ","p_videoepnumber=");
// Using regex:
string.replaceAll("(p_videoepnumber=)(\\s+)","$1");

Demo

So, in this String :

p_videoid=119110,p_videoepnumber= 0,p_videoairdate=NOT NULL,videoseason=null

You want to catch p_videoepnumber= 0, .

The first idea that comes to mind is attempting to match p_videoepnumber= .*, , but it would actually capture p_videoepnumber= 0,p_videoairdate=NOT NULL, .

As you want to stop at the first comma, you will actually use a reluctant matcher. You will actually match p_videoepnumber= .*?, (Notice the extra ? ).

From that point, here is the commented code :

String req = "p_videoid=119110,p_videoepnumber= 0,p_videoairdate=NOT NULL,videoseason=null";

// Add parenthesis to indicate the group to capture
String regex = "(p_videoepnumber= .*?,)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(req);

// find() finds the next subsequence of the input sequence that matches the pattern.
if (matcher.find()) {
    // Displays the number of found groups : 1
    System.out.println(matcher.groupCount());
    // Displays the found group : p_videoepnumber= 0,
    System.out.println(matcher.group(1));
    // Replace " " with "" in the first group and display the result : 
    // p_videoid=119110,p_videoepnumber=0,p_videoairdate=NOT NULL,videoseason=null
    System.out.println(matcher.replaceFirst(matcher.group(1).replaceAll(" ", "")));
} else {
    System.out.println("No match");
}

You can use the String.split method:

String[] splitedString = "yourString".split(",");

and then you can iterate each element of your String array and find the element that start with p_videoepnumber

for(int i=0; i<splitedString.length; i++){
    if(splitedString[i].startsWith("p_videoepnumber"){
        splitedString[i] = splitedString[i].trim();
     }
}
String parsedString = String.join(",", splitedString);

This RegEx might help you to do so. It simply creates a group in between two boundaries where the spaces might be:

p_videoepnumber=(\s+)[0-9]+,
  • You can simply call those spaces with $1 and replace it with an empty string '' .

  • You can also add additional boundaries to this expression, if you wish.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM