Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
webmagic
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
沈俊林
webmagic
Commits
5d9fda06
Commit
5d9fda06
authored
Dec 21, 2013
by
Yihua Huang
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #51 from code4craft/test
Update RegexSelector.java
parents
d9a15ad6
2b46b11e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
12 deletions
+16
-12
RegexSelector.java
...in/java/us/codecraft/webmagic/selector/RegexSelector.java
+4
-4
RegexSelectorTest.java
...ava/us/codecraft/webmagic/selector/RegexSelectorTest.java
+12
-8
No files found.
webmagic-core/src/main/java/us/codecraft/webmagic/selector/RegexSelector.java
View file @
5d9fda06
...
@@ -26,12 +26,12 @@ public class RegexSelector implements Selector {
...
@@ -26,12 +26,12 @@ public class RegexSelector implements Selector {
if
(
StringUtils
.
isBlank
(
regexStr
))
{
if
(
StringUtils
.
isBlank
(
regexStr
))
{
throw
new
IllegalArgumentException
(
"regex must not be empty"
);
throw
new
IllegalArgumentException
(
"regex must not be empty"
);
}
}
if
(!
StringUtils
.
contains
(
regexStr
,
"("
)
&&
!
StringUtils
.
contains
(
regexStr
,
")"
))
{
// Check bracket for regex group. Add default group 1 if there is no group.
// Only check if there exists the valid left parenthesis, leave regexp validation for Pattern.
if
(
StringUtils
.
countMatches
(
regexStr
,
"("
)
-
StringUtils
.
countMatches
(
regexStr
,
"\\("
)
==
StringUtils
.
countMatches
(
regexStr
,
"(?:"
)
-
StringUtils
.
countMatches
(
regexStr
,
"\\(?:"
))
{
regexStr
=
"("
+
regexStr
+
")"
;
regexStr
=
"("
+
regexStr
+
")"
;
}
}
if
(!
StringUtils
.
contains
(
regexStr
,
"("
)
||
!
StringUtils
.
contains
(
regexStr
,
")"
))
{
throw
new
IllegalArgumentException
(
"regex must have capture group 1"
);
}
this
.
regexStr
=
regexStr
;
this
.
regexStr
=
regexStr
;
try
{
try
{
regex
=
Pattern
.
compile
(
regexStr
,
Pattern
.
DOTALL
|
Pattern
.
CASE_INSENSITIVE
);
regex
=
Pattern
.
compile
(
regexStr
,
Pattern
.
DOTALL
|
Pattern
.
CASE_INSENSITIVE
);
...
...
webmagic-core/src/test/java/us/codecraft/webmagic/selector/RegexSelectorTest.java
View file @
5d9fda06
package
us
.
codecraft
.
webmagic
.
selector
;
package
us
.
codecraft
.
webmagic
.
selector
;
import
junit.framework
.Assert
;
import
org.junit
.Assert
;
import
org.junit.Test
;
import
org.junit.Test
;
/**
/**
...
@@ -8,14 +8,18 @@ import org.junit.Test;
...
@@ -8,14 +8,18 @@ import org.junit.Test;
*/
*/
public
class
RegexSelectorTest
{
public
class
RegexSelectorTest
{
@Test
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
test
InvalidRegex
()
{
public
void
test
RegexWithSingleLeftBracket
()
{
String
regex
=
"\\d+("
;
String
regex
=
"\\d+("
;
try
{
new
RegexSelector
(
regex
);
new
RegexSelector
(
regex
);
}
Assert
.
assertNotNull
(
regex
);
}
catch
(
Exception
e
)
{
}
@Test
public
void
testRegexWithLeftBracketQuoted
()
{
String
regex
=
"\\(.+"
;
String
source
=
"(hello world"
;
RegexSelector
regexSelector
=
new
RegexSelector
(
regex
);
String
select
=
regexSelector
.
select
(
source
);
Assert
.
assertEquals
(
source
,
select
);
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment